300 lines
9.5 KiB
C
300 lines
9.5 KiB
C
#include <rtthread.h>
|
||
#include <finsh.h>
|
||
#include "common.h"
|
||
#include "param_config.h"
|
||
#include "ble_pub.h"
|
||
#include <rtthread.h>
|
||
#include <rtdevice.h>
|
||
#include "wlan_ui_pub.h"
|
||
#include "sys_ctrl_pub.h"
|
||
#include "sys_ctrl.h"
|
||
#include "target_util_pub.h"
|
||
#include "drv_model_pub.h"
|
||
#include "md5.h"
|
||
#include "app_temperature.h"
|
||
#include "BkDriverFlash.h"
|
||
#include "manual_ps_pub.h"
|
||
#ifdef PKG_USING_EASYFLASH
|
||
#include "easyflash.h"
|
||
|
||
#endif
|
||
|
||
#include "mqtt_client.h"
|
||
|
||
extern mqtt_client client;
|
||
|
||
//WIFI信息
|
||
extern char last_ssid[33] ;
|
||
extern char last_password[65];
|
||
char last_bssid[65];
|
||
char last_channel[65];
|
||
char last_security[65];
|
||
|
||
network_InitTypeDef_adv_st adv_config;
|
||
|
||
//MQTT账号密码
|
||
char mqtt_username[128]={0};
|
||
char mqtt_password[128]={0};
|
||
|
||
extern char g_sta_mac[6];
|
||
|
||
//MQTT上传温度
|
||
void mqtt_upload_temperature(uint16_t t_value){
|
||
uint16_t xy = t_value/10; //整数
|
||
// uint16_t y = xy % 10 ;//个位
|
||
// uint16_t x = (xy / 10) % 10 ;//十位
|
||
// uint16_t z = t_value/10%10 ;//小数位
|
||
char msg[128];
|
||
char *topic_properties =(char *)os_malloc(128);
|
||
char device_id [64];
|
||
rt_memset(device_id,0,sizeof(device_id));
|
||
rt_sprintf(device_id,"%s_%02X%02X%02X%02X%02X%02X",
|
||
PRODUCTID,g_sta_mac[0],g_sta_mac[1],g_sta_mac[2],g_sta_mac[3],g_sta_mac[4],g_sta_mac[5]);
|
||
|
||
os_memset(topic_properties,0,128);
|
||
rt_sprintf(topic_properties, "/%s/%s/properties/report", PRODUCTID, device_id);
|
||
|
||
rt_sprintf(msg, "{\"properties\": {\"temperature\":%d}}", xy);
|
||
paho_mqtt_publish(&client, QOS1, topic_properties, msg, strlen(msg));
|
||
bk_printf("MQTT publish: %s\n", msg);
|
||
bk_printf("device_id: %s\n", device_id);
|
||
os_free(topic_properties);
|
||
}
|
||
|
||
void mqtt_upload_boxbat(uint16_t b_value){
|
||
uint16_t xy = b_value; //整数
|
||
// uint16_t y = xy % 10 ;//个位
|
||
// uint16_t x = (xy / 10) % 10 ;//十位
|
||
// uint16_t z = t_value/10%10 ;//小数位
|
||
char msg[128];
|
||
char *topic_properties =(char *)os_malloc(128);
|
||
char device_id [64];
|
||
rt_memset(device_id,0,sizeof(device_id));
|
||
rt_sprintf(device_id,"%s_%02X%02X%02X%02X%02X%02X",
|
||
PRODUCTID,g_sta_mac[0],g_sta_mac[1],g_sta_mac[2],g_sta_mac[3],g_sta_mac[4],g_sta_mac[5]);
|
||
|
||
os_memset(topic_properties,0,128);
|
||
rt_sprintf(topic_properties, "/%s/%s/properties/report", PRODUCTID, device_id);
|
||
|
||
rt_sprintf(msg, "{\"properties\": {\"box_bat\":%d}}", xy);
|
||
paho_mqtt_publish(&client, QOS1, topic_properties, msg, strlen(msg));
|
||
bk_printf("MQTT publish: %s\n", msg);
|
||
bk_printf("device_id: %s\n", device_id);
|
||
os_free(topic_properties);
|
||
}
|
||
|
||
void mqtt_upload_bat(uint16_t b_value){
|
||
uint16_t xy = b_value; //整数
|
||
// uint16_t y = xy % 10 ;//个位
|
||
// uint16_t x = (xy / 10) % 10 ;//十位
|
||
// uint16_t z = t_value/10%10 ;//小数位
|
||
char msg[128];
|
||
char *topic_properties =(char *)os_malloc(128);
|
||
char device_id [64];
|
||
rt_memset(device_id,0,sizeof(device_id));
|
||
rt_sprintf(device_id,"%s_%02X%02X%02X%02X%02X%02X",
|
||
PRODUCTID,g_sta_mac[0],g_sta_mac[1],g_sta_mac[2],g_sta_mac[3],g_sta_mac[4],g_sta_mac[5]);
|
||
|
||
os_memset(topic_properties,0,128);
|
||
rt_sprintf(topic_properties, "/%s/%s/properties/report", PRODUCTID, device_id);
|
||
|
||
rt_sprintf(msg, "{\"properties\": {\"bat\":%d}}", xy);
|
||
paho_mqtt_publish(&client, QOS1, topic_properties, msg, strlen(msg));
|
||
bk_printf("MQTT publish: %s\n", msg);
|
||
bk_printf("device_id: %s\n", device_id);
|
||
os_free(topic_properties);
|
||
}
|
||
|
||
void connnect_wifi(char *oob_ssid,char *connect_key){//连接wifi
|
||
/*定义一个结构体,用于传入参数*/
|
||
network_InitTypeDef_st wNetConfig;
|
||
int len;
|
||
|
||
/*把这个结构体置空*/
|
||
os_memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
|
||
|
||
/*检查SSID的长度,不能超过32字节*/
|
||
len = os_strlen(oob_ssid);
|
||
if(SSID_MAX_LEN < len)
|
||
{
|
||
bk_printf("ssid name more than 32 Bytes\r\n");
|
||
return;
|
||
}
|
||
|
||
/*将SSID跟密码传入结构体*/
|
||
os_strcpy((char *)wNetConfig.wifi_ssid, oob_ssid);
|
||
os_strcpy((char *)wNetConfig.wifi_key, connect_key);
|
||
|
||
/*当前为客户端模式*/
|
||
wNetConfig.wifi_mode = BK_STATION;
|
||
/*采用DHCP CLIENT的方式获得,从路由器动态获取IP地址*/
|
||
wNetConfig.dhcp_mode = DHCP_CLIENT;
|
||
wNetConfig.wifi_retry_interval = 100;
|
||
bk_printf("ssid:%s key:%s\r\n", wNetConfig.wifi_ssid, wNetConfig.wifi_key);
|
||
/*启动WiFi连接*/
|
||
bk_wlan_start(&wNetConfig);
|
||
//rt_thread_mdelay(5000);
|
||
}
|
||
|
||
// 读取最新联网信息
|
||
void wifi_read_info(char *ssid, size_t ssid_len, char *password, size_t pass_len,
|
||
char *bssid, size_t bssid_len, char *channel, size_t channel_len, char *security, size_t security_len){
|
||
|
||
char *t_ssid = ef_get_env(BLE_WIFI_FLASH_KEY_SSID);
|
||
char *t_pass = ef_get_env(BLE_WIFI_FLASH_KEY_PASS);
|
||
char *t_bssid = ef_get_env(BLE_WIFI_FLASH_KEY_BSSID);
|
||
char *t_channel = ef_get_env(BLE_WIFI_FLASH_KEY_CHANNEL);
|
||
char *t_security = ef_get_env(BLE_WIFI_FLASH_KEY_SECURITY);
|
||
|
||
if(t_ssid) {
|
||
strncpy(last_ssid, t_ssid, ssid_len - 1);
|
||
last_ssid[ssid_len - 1] = '\0';
|
||
}
|
||
if(t_pass) {
|
||
strncpy(last_password, t_pass, pass_len - 1);
|
||
last_password[pass_len - 1] = '\0';
|
||
}
|
||
if (t_bssid) {
|
||
strncpy(bssid, t_bssid, bssid_len - 1);
|
||
bssid[bssid_len - 1] = '\0';
|
||
}
|
||
if (t_channel) {
|
||
strncpy(channel, t_channel, channel_len - 1);
|
||
channel[channel_len - 1] = '\0';
|
||
}
|
||
if (t_security) {
|
||
strncpy(security, t_security, security_len - 1);
|
||
security[security_len - 1] = '\0';
|
||
}
|
||
}
|
||
|
||
//将flash存储的mac地址字符串转换为二进制格式
|
||
void mac_string_to_binary(const char *mac_str, uint8_t *mac_bin) {
|
||
if (!mac_str || !mac_bin) return;
|
||
|
||
char temp[18];
|
||
strncpy(temp, mac_str, sizeof(temp)-1);
|
||
temp[sizeof(temp)-1] = '\0';
|
||
|
||
for (int i = 0; temp[i]; i++) {
|
||
temp[i] = toupper(temp[i]);
|
||
}
|
||
|
||
char *p = temp;
|
||
char *end = temp + strlen(temp);
|
||
while (p < end) {
|
||
if (*p == ':' || *p == '-') {
|
||
memmove(p, p+1, end - p);
|
||
end--;
|
||
} else {
|
||
p++;
|
||
}
|
||
}
|
||
|
||
if (strlen(temp) != 12) {
|
||
bk_printf("Invalid MAC length: %d\n", strlen(temp));
|
||
return;
|
||
}
|
||
|
||
for (int i = 0; i < 6; i++) {
|
||
char byte_str[3] = { temp[i*2], temp[i*2+1], '\0' };
|
||
char *endptr;
|
||
mac_bin[i] = (uint8_t)strtol(byte_str, &endptr, 16);
|
||
|
||
if (*endptr != '\0') {
|
||
bk_printf("MAC conversion error at position %d\n", i);
|
||
}
|
||
}
|
||
|
||
}
|
||
//int fast_connect_try = 0;
|
||
//快速连接
|
||
void fast_connect_wifi(){
|
||
// 检查是否有效
|
||
// if (fci.ssid[0] != 0 && strlen((char*)fci.ssid) < 32) {
|
||
uint8_t mac_bin[6] ={0};
|
||
int sec_value = atoi(last_security);
|
||
|
||
memset(&adv_config, 0, sizeof(adv_config));
|
||
strncpy(adv_config.ap_info.ssid, last_ssid, sizeof(adv_config.ap_info.ssid));
|
||
adv_config.ap_info.ssid[sizeof(adv_config.ap_info.ssid)-1] = '\0';
|
||
|
||
mac_string_to_binary(last_bssid, mac_bin);
|
||
memcpy(adv_config.ap_info.bssid, mac_bin, 6);
|
||
|
||
adv_config.ap_info.channel = (uint8_t)atoi(last_channel);
|
||
|
||
if (sec_value >= BK_SECURITY_TYPE_NONE && sec_value <= BK_SECURITY_TYPE_AUTO) {
|
||
adv_config.ap_info.security = (wlan_sec_type_t)sec_value;
|
||
} else {
|
||
adv_config.ap_info.security = BK_SECURITY_TYPE_AUTO;
|
||
}
|
||
|
||
strncpy(adv_config.key, last_password, sizeof(adv_config.key));
|
||
adv_config.key[sizeof(adv_config.key)-1] = '\0';
|
||
adv_config.key_len = strlen(last_password);
|
||
adv_config.dhcp_mode = DHCP_CLIENT;
|
||
|
||
bk_printf(" SSID: %s\n", adv_config.ap_info.ssid);
|
||
bk_printf(" BSSID: %02X:%02X:%02X:%02X:%02X:%02X\n",
|
||
adv_config.ap_info.bssid[0], adv_config.ap_info.bssid[1],
|
||
adv_config.ap_info.bssid[2], adv_config.ap_info.bssid[3],
|
||
adv_config.ap_info.bssid[4], adv_config.ap_info.bssid[5]);
|
||
bk_printf(" Channel: %d\n", adv_config.ap_info.channel);
|
||
bk_printf(" Security: %d\n", adv_config.ap_info.security);
|
||
|
||
bk_wlan_start_sta_adv(&adv_config);
|
||
// } else {
|
||
// fast_connect_try = 0;
|
||
// //fast_connect_failed = 1;
|
||
// bk_printf("No fast connect info, fallback to normal connect\n");
|
||
// // 调用普通连接流程
|
||
// }
|
||
}
|
||
|
||
//wifi初始化连接
|
||
void wifi_init(){
|
||
wifi_read_info(last_ssid, sizeof(last_ssid), last_password, sizeof(last_password),
|
||
last_bssid, sizeof(last_bssid), last_channel, sizeof(last_channel), last_security, sizeof(last_security));
|
||
|
||
//fast_connect_wifi();
|
||
|
||
if(last_ssid[0] != '\0' && last_password[0] != '\0') {
|
||
bk_printf("Auto connect WiFi: SSID=%s, PASSWORD=%s\r\n", last_ssid, last_password);
|
||
bk_printf("BSSID: %s\n", last_bssid);
|
||
bk_printf("Channel: %s\n", last_channel);
|
||
bk_printf("Security: %s\n", last_security);
|
||
connnect_wifi(last_ssid, last_password);
|
||
}else{
|
||
bk_printf("NOT WiFi INFO\r\n");
|
||
}
|
||
}
|
||
|
||
void poka_client_username(time_t now){
|
||
rt_tick_t tick = rt_tick_get();
|
||
uint32_t ms_part = (tick % RT_TICK_PER_SECOND) * 1000 / RT_TICK_PER_SECOND;
|
||
uint64_t ms = ((uint64_t)now) * 1000 + ms_part;
|
||
snprintf(mqtt_username, sizeof(mqtt_username), "%s|%llu", SECUREID_MQTT, (unsigned long long)ms);
|
||
}
|
||
|
||
void poka_client_password(){
|
||
char input[512];
|
||
unsigned char md5sum[16];
|
||
int i;
|
||
|
||
// 拼接 username 和 SECUREKEY_MQTT
|
||
snprintf(input, sizeof(input), "%s|%s", mqtt_username, SECUREKEY_MQTT);
|
||
MD5_CTX ctx;
|
||
MMD5Init(&ctx);
|
||
MMD5Update(&ctx, (unsigned char *)input, strlen(input));
|
||
MMD5Final(&ctx,md5sum);
|
||
|
||
|
||
for(i = 0; i < 16; i++)
|
||
sprintf(mqtt_password + i * 2, "%02x", md5sum[i]);
|
||
mqtt_password[32] = '\0';
|
||
|
||
rt_kprintf("username=%s\r\n",mqtt_username);
|
||
rt_kprintf("password=%s\r\n", mqtt_password);
|
||
}
|