helloyifa 31f179cb76 init
2025-05-15 14:19:56 +08:00

1043 lines
37 KiB
C
Executable File

/* WiFi station Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <common/bk_include.h>
#include "poka_client.h"
#include <common/bk_include.h>
#include <os/os.h>
#include <os/mem.h>
#include <os/str.h>
//#include "lwip/sockets.h"
#include "uart_pub.h"
//#include "lwip/sockets.h"
//#include "lwip/ip_addr.h"
//#include "lwip/inet.h"
#include "poka_client.h"
#include "mqtt_instance.h"
#include "iot_export_mqtt.h"
//#include "time/ntp.h"
//#include "time/time.h"
#include "cJSON.h"
#include "md5.h"
#include <driver/timer.h>
#include "bk_misc.h"
#include "app_time.h"
#include "time/time.h"
#include <time/ntp.h>
#include <stdlib.h>
#include <stdio.h>
#define TAG "poka_client"
#include "ble_client.h"
#include "ilock_config.h"
#include "ilock_client.h"
#define LOGI(...) BK_LOGI(TAG, ##__VA_ARGS__)
#define LOGE(...) BK_LOGE(TAG, ##__VA_ARGS__)
#define LOGW(...) BK_LOGW(TAG, ##__VA_ARGS__)
#define LOGD(...) BK_LOGD(TAG, ##__VA_ARGS__)
//#define TMQTT_SERVER "J2QAWBBRG5.iotcloud.tencentdevices.com"
//#define TMQTT_PORT 1883
//#define TCLIENT_ID "J2QAWBBRG5ismart_lock1"
//#define TUSER_NAME "J2QAWBBRG5ismart_lock1;12010126;1a070;1706457600"
//#define TPASSWORD "536b725a3fcc7cca94652ae4e7b95497b66c6aac50ff0ef5b9e1a9a61d286076;hmacsha256"
//#define TOPIC_ACTION "$thing/down/action/J2QAWBBRG5/ismart_lock1"
//#define TOPIC_ACTION_REPLAY "$thing/up/action/J2QAWBBRG5/ismart_lock1"
//#define PRODUCTID "LFV1"
//#define SECUREID "poka"
//#define SECUREKEY "Poka&Lock12345"
//#define TMQTT_SERVER "svr.xa-poka.com"
//#define TMQTT_PORT 1883
//#define TCLIENT_ID "LFV1_A0FE111034600003"
#define TUSER_NAME "J2QAWBBRG5ismart_lock1;12010126;1a070;1706457600"
#define TPASSWORD "536b725a3fcc7cca94652ae4e7b95497b66c6aac50ff0ef5b9e1a9a61d286076;hmacsha256"
#define TOPIC_ACTION "$thing/down/action/J2QAWBBRG5/ismart_lock1"
//#define TOPIC_ACTION_REPLAY "$thing/up/action/J2QAWBBRG5/ismart_lock1"
int MQTT_Socket = 0;
static char mqtt_clientid[128] = {0};
static char mqtt_username[128] = {0};
static char mqtt_password[128] = {0};
static char ilock_device_id[32] = {0};
//当前正在执行的iot_message_id
static char iot_message_id [64] = {0};
static poka_client_cb_ops_t *event_cb;
static beken_timer_t timer_reboot_handle;
char * poka_client_get_device_id(){
return ilock_device_id;
}
char * poka_client_get_message_id(){
return iot_message_id;
}
/**
*
*
时间 2023-11-04 15:25:38.364
日志类型 设备事件上报
method "event_post"
clientToken "d9dc55d3-a89c-4e3c-9166-ba689c047909"
version "1.0"
eventId "unlock_remote_result"
timestamp 1699082738180
params {"result":1}
* // method: control_reply
message_len = HAL_Snprintf(message, sizeof(message),
"{\"method\":\"control_reply\", \"code\":0, \"clientToken\":\"%s\"}", token);
*/
int mqtt_report_payload(char *msg, char *method, char *clientToken, int result)
{
int ret = 0;
/*topic's json data: {"id":"requestId" ,"params":{"_sys_device_mid":mid,"_sys_device_pid":pid }}*/
#if 1
ret = os_snprintf(msg,
384,
"{\"method\":\"action_reply\", \"code\":0, \"clientToken\":\"%s\"}",
method,
clientToken,
result);
BK_LOGE(TAG,"%s\n",msg);
#endif
return ret;
}
void poka_client_replay_invoke(char* functionId, char* messageId, char* result, char * output){
if(messageId == NULL || strlen(iot_message_id) == 0){
return;
}
char *topic_replay_invoke = (char *)os_malloc(128);
if(topic_replay_invoke == NULL){
BK_LOGE(TAG,"replay_invoke: os_malloc fail\n" );
return;
}
os_memset(topic_replay_invoke,0,128);
//char topic_replay_invoke[128] ={0};
sprintf(topic_replay_invoke,"/%s/%s/function/invoke/reply",PRODUCTID,ilock_device_id);
//char playload[128] = {0};
//sprintf(playload,"{\"messageId\":\"%s\",\"success\":%s}",messageId,result);
//mqtt_publish(topic_replay_invoke, IOTX_MQTT_QOS0, &playload, strlen(playload));
// BK_LOGE(TAG,"replay_invoke: 2\n" );
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "messageId", messageId);
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
cJSON_AddStringToObject(root, "success", result);
if(functionId != NULL && strlen(functionId)>0){
cJSON_AddStringToObject(root, "functionId", functionId);
}
if(output != NULL && strlen(output)>0){
cJSON_AddStringToObject(root, "output", output);
}
char *jsonStr = cJSON_Print(root);
BK_LOGE(TAG,"replay invoke messageId:%s\n",messageId);
mqtt_publish(topic_replay_invoke, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
cJSON_free(jsonStr);
os_free(topic_replay_invoke);
//free(messageId);
//清空最近的messageid
memset(iot_message_id,0,sizeof(iot_message_id));
//BK_LOGE(TAG,"replay_invoke: 2\n" );
cJSON_Delete(root);
}
int generate_random_number(int min, int max) {
//return rand() % (max min + 1) + min;
return 0;
}
void mqtt_subscribe_callback(char *topic, int topic_len, void *payload, int payload_len, void *ctx){
//BK_LOGE(TAG,"subscribe_callback:%s",topic);
//BK_LOGE(TAG,"subscribe_callback:%s",payload);
//BK_LOGE(TAG,"mqtt_subscribe_callback\n");
cJSON* json_payload = cJSON_Parse(payload);
cJSON* messageId = cJSON_GetObjectItem(json_payload,"messageId");
//BK_LOGE(TAG,"\nmessageId:%s\n",messageId->valuestring);
poka_client_replay_invoke(NULL,messageId->valuestring,FUN_TRUE,NULL);
cJSON_Delete(json_payload);
#if 0
cJSON* json_payload = cJSON_Parse(payload);
cJSON* method = cJSON_GetObjectItem(json_payload,"method");
cJSON* client_token = cJSON_GetObjectItem(json_payload,"clientToken");
//char* pvalue = LITE_json_value_of("method", payload);
BK_LOGE(TAG,"method->%s\n",method->valuestring);
//pvalue = LITE_json_value_of("clientToken", pvalue);
BK_LOGE(TAG,"clientToken->%s\n",client_token->valuestring);
//char resp[384] = {0x00};
//mqtt_report_payload(resp,"action_reply",client_token->valuestring,0);
//mqtt_publish(TOPIC_ACTION_REPLAY, IOTX_MQTT_QOS0, &resp, strlen(resp));
#endif
}
void poka_client_report_event(char*event,char* data_result){
/*
{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"随机消息ID",
"data":100 //上报数据,类型与物模型事件中定义的类型一致
}*/
//char topic_event[128] ={0};
char *topic_event = (char *)os_malloc(128);
if(topic_event == NULL){
BK_LOGE(TAG,"replay_invoke: os_malloc fail\n" );
return;
}
os_memset(topic_event,0,128);
sprintf(topic_event,"/%s/%s/event/%s",PRODUCTID,ilock_device_id,event);
//BK_LOGE(TAG,"topic_event:%s\n",topic_event);
cJSON *root = cJSON_CreateObject();
char str_msgid[24] = {0};
sprintf(str_msgid,"%d",rand());
cJSON_AddStringToObject(root, "messageId", str_msgid);
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
if(data_result!=NULL && strlen(data_result) > 0){
cJSON_AddStringToObject(root, "data", data_result);
}
char *jsonStr = cJSON_Print(root);
BK_LOGE(TAG,"jsonStr:%s\n",jsonStr);
mqtt_publish(topic_event, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
cJSON_free(jsonStr);
os_free(topic_event);
//os_free(str_msgid);
cJSON_Delete(root);
}
void poka_client_report_event_int(char*event,int result){
/*
{
"timestamp":1601196762389, //毫秒时间戳
"messageId":"随机消息ID",
"data":100 //上报数据,类型与物模型事件中定义的类型一致
}*/
char *topic_event = (char *)os_malloc(128);
if(topic_event == NULL){
BK_LOGE(TAG,"replay_invoke: os_malloc fail\n" );
return;
}
os_memset(topic_event,0,128);
sprintf(topic_event,"/%s/%s/event/%s",PRODUCTID,ilock_device_id,event);
//BK_LOGE(TAG,"topic_event:%s\n",topic_event);
cJSON *root = cJSON_CreateObject();
char str_msgid[24] = {0};
sprintf(str_msgid,"%d",rand());
cJSON_AddStringToObject(root, "messageId", str_msgid);
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
cJSON_AddNumberToObject(root, "data", result);
char *jsonStr = cJSON_Print(root);
BK_LOGE(TAG,"jsonStr:%s\n",jsonStr);
mqtt_publish(topic_event, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
cJSON_free(jsonStr);
os_free(topic_event);
cJSON_Delete(root);
}
void poka_client_report_properties(char *version ,int lockin_state,int door_state,int lock_state,int batt_state, int batt_value,int disable_lock,int power_supply){
//char topic_properties[128] ={0};
char *topic_properties = (char *)os_malloc(128);
if(topic_properties == NULL){
BK_LOGE(TAG,"replay_invoke: os_malloc fail\n" );
return;
}
os_memset(topic_properties,0,128);
sprintf(topic_properties,"/%s/%s/properties/report",PRODUCTID,ilock_device_id);
cJSON *root = cJSON_CreateObject();
char str_msgid[24] = {0};
sprintf(str_msgid,"%d",rand());
cJSON_AddStringToObject(root, "messageId", str_msgid);
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
cJSON *properties = cJSON_CreateObject();
cJSON_AddNumberToObject(properties, "battery_cap", batt_value);
cJSON_AddNumberToObject(properties, "battery_state", batt_state);
cJSON_AddNumberToObject(properties, "door_state", door_state);
cJSON_AddNumberToObject(properties, "lockedin_state", lockin_state);
cJSON_AddNumberToObject(properties, "lock_state", lock_state);
//cJSON_AddStringToObject(properties, "ilock_version", version);
cJSON_AddNumberToObject(properties, "disable_lock", disable_lock);
cJSON_AddNumberToObject(properties, "power_supply", power_supply);
cJSON_AddItemToObjectCS(root,"properties",properties);
char *jsonStr = cJSON_Print(root);
BK_LOGE(TAG,"playload:%d\n",strlen(jsonStr));
//BK_LOGE(TAG,"playload:%s\n",jsonStr);
mqtt_publish(topic_properties, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
os_free(topic_properties);
cJSON_free(jsonStr);
//cJSON_Delete(properties);
cJSON_Delete(root);
//os_free( playload );
}
void poka_client_report_properties_keyvalue(char* key, char *version){
//char topic_properties[128] ={0};
char *topic_properties = (char *)os_malloc(128);
if(topic_properties == NULL){
BK_LOGE(TAG,"replay_invoke: os_malloc fail\n" );
return;
}
os_memset(topic_properties,0,128);
sprintf(topic_properties,"/%s/%s/properties/report",PRODUCTID,ilock_device_id);
cJSON *root = cJSON_CreateObject();
char str_msgid[24] = {0};
sprintf(str_msgid,"%d",rand());
cJSON_AddStringToObject(root, "messageId", str_msgid);
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
cJSON *properties = cJSON_CreateObject();
cJSON_AddStringToObject(properties, key, version);
cJSON_AddItemToObjectCS(root,"properties",properties);
char *jsonStr = cJSON_Print(root);
BK_LOGE(TAG,"playload:%d\n",strlen(jsonStr));
//BK_LOGE(TAG,"playload:%s\n",jsonStr);
mqtt_publish(topic_properties, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
os_free(topic_properties);
//os_free(str_msgid);
cJSON_free(jsonStr);
//cJSON_Delete(properties);
cJSON_Delete(root);
//os_free( playload );
}
void poka_client_report_tags_keyvalue(char* key, char *value){
char *topic_tags = (char *)os_malloc(128);
if(topic_tags == NULL){
BK_LOGE(TAG,"report_tags: os_malloc fail\n" );
return;
}
os_memset(topic_tags,0,128);
sprintf(topic_tags,"/%s/%s/tags",PRODUCTID,ilock_device_id);
cJSON *root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
cJSON *tags = cJSON_CreateObject();
cJSON_AddStringToObject(tags, key, value);
cJSON_AddItemToObjectCS(root,"tags",tags);
char *jsonStr = cJSON_Print(root);
BK_LOGE(TAG,"playload:%d\n",strlen(jsonStr));
//BK_LOGE(TAG,"playload:%s\n",jsonStr);
mqtt_publish(topic_tags, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
os_free(topic_tags);
cJSON_free(jsonStr);
cJSON_Delete(root);
}
void poka_client_report_device_tags(char* version, char* wifi_mac, char *wifi_ssid, char* wifi_pwd,char* ap_bssid,char* rssi){
char *topic_tags = (char *)os_malloc(128);
if(topic_tags == NULL){
BK_LOGE(TAG,"report_tags: os_malloc fail\n" );
return;
}
os_memset(topic_tags,0,128);
sprintf(topic_tags,"/%s/%s/tags",PRODUCTID,ilock_device_id);
cJSON *root = cJSON_CreateObject();
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
cJSON *tags = cJSON_CreateObject();
cJSON_AddStringToObject(tags, "ilock_back_version", version);
//cJSON_AddStringToObject(tags, "ilock_body_version", "77777777777");
//cJSON_AddStringToObject(tags, "ilock_front_version", "8888888888");
cJSON_AddStringToObject(tags, "ilock_wifi_ssid", wifi_ssid);
cJSON_AddStringToObject(tags, "ilock_wifi_pwd", wifi_pwd);
cJSON_AddStringToObject(tags, "ilock_wifi_mac", wifi_mac);
cJSON_AddStringToObject(tags, "ilock_wifi_bssid", ap_bssid);
cJSON_AddStringToObject(tags, "ilock_wifi_rssi", rssi);
cJSON_AddItemToObjectCS(root,"tags",tags);
char *jsonStr = cJSON_Print(root);
//BK_LOGE(TAG,"playload:%d\n",strlen(jsonStr));
mqtt_publish(topic_tags, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
os_free(topic_tags);
cJSON_free(jsonStr);
cJSON_Delete(root);
}
void poka_client_report_properties_keyvalue_int(char* key, int value){
//char topic_properties[128] ={0};
char *topic_properties = (char *)os_malloc(128);
if(topic_properties == NULL){
BK_LOGE(TAG,"replay_invoke: os_malloc fail\n" );
return;
}
os_memset(topic_properties,0,128);
sprintf(topic_properties,"/%s/%s/properties/report",PRODUCTID,ilock_device_id);
cJSON *root = cJSON_CreateObject();
char str_msgid[24] = {0};
sprintf(str_msgid,"%d",rand());
cJSON_AddStringToObject(root, "messageId", str_msgid);
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
cJSON *properties = cJSON_CreateObject();
cJSON_AddNumberToObject(properties, key,value);
cJSON_AddItemToObjectCS(root,"properties",properties);
char *jsonStr = cJSON_Print(root);
BK_LOGE(TAG,"playload:%d\n",strlen(jsonStr));
//BK_LOGE(TAG,"playload:%s\n",jsonStr);
mqtt_publish(topic_properties, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
os_free(topic_properties);
cJSON_free(jsonStr);
cJSON_Delete(root);
}
void poka_client_reply_properties(char * messageId){
//char topic_properties[128] ={0};
char *topic_properties = (char *)os_malloc(128);
if(topic_properties == NULL){
BK_LOGE(TAG,"replay_invoke: os_malloc fail\n" );
return;
}
os_memset(topic_properties,0,128);
sprintf(topic_properties,"/%s/%s/properties/read/reply",PRODUCTID,ilock_device_id);
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "messageId", messageId);
cJSON_AddNumberToObject(root, "timestamp", app_time_timestamp_ms());
cJSON *properties = cJSON_CreateObject();
cJSON_AddNumberToObject(properties, "battery_cap", dev_info_lasted.lock_batt_value);
cJSON_AddNumberToObject(properties, "battery_state", dev_info_lasted.lock_batt_state);
cJSON_AddNumberToObject(properties, "door_state", dev_info_lasted.lock_door_state);
cJSON_AddNumberToObject(properties, "lock_state", dev_info_lasted.lock_lock_state);
cJSON_AddNumberToObject(properties, "lockedin_state", dev_info_lasted.lock_lockedin_state);
//cJSON_AddStringToObject(properties, "ilock_version", ILOCK_SOFT_VERSION);
//cJSON_AddStringToObject(properties, "lock_params", dev_info_lasted.lock_params);
cJSON_AddNumberToObject(properties, "disable_lock", dev_info_lasted.lock_disable_lock_state);
cJSON_AddItemToObjectCS(root,"properties",properties);
char *jsonStr = cJSON_Print(root);
BK_LOGE(TAG,"playload:%d\n",strlen(jsonStr));
//BK_LOGE(TAG,"playload:%s\n",jsonStr);
mqtt_publish(topic_properties, IOTX_MQTT_QOS0, jsonStr, strlen(jsonStr));
os_free(topic_properties);
cJSON_free(jsonStr);
//cJSON_Delete(properties);
cJSON_Delete(root);
//os_free( playload );
}
void mqtt_subscribe_invoke_callback(char *topic, int topic_len, void *payload, int payload_len, void *ctx){
//BK_LOGE(TAG,"subscribe_callback:%s",topic);
//BK_LOGE(TAG,"p:%s",payload);
//BK_LOGE(TAG,"mqtt_subscribe_callback\n");
#if 1
if (event_cb != NULL){
event_cb->event_cloud();
//复位定时器
rtos_reload_timer(&timer_reboot_handle);
}
cJSON* json_payload = cJSON_Parse(payload);
cJSON* messageId = cJSON_GetObjectItem(json_payload,"messageId");
memset(iot_message_id,0,sizeof(iot_message_id));
strcpy(iot_message_id,messageId->valuestring);
//BK_LOGE(TAG,"\ninvoke_callback messageId:%s\n",iot_message_id);
//functionIdinputs_list
cJSON* functionId = cJSON_GetObjectItem(json_payload,"functionId");
//BK_LOGE(TAG,"\ninvoke_callback functionId:%s\n",functionId->valuestring);
if (strcmp(functionId->valuestring, EVENT_REMOTE_VIDEO) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs"); // clientlist 是使用 cjson对象
if (NULL != inputs)
{
int audio = 0;
char *token = NULL;
char *license = NULL;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "audio") == 0)
{
audio = cJSON_GetObjectItem(inputs_list, "value")->valueint;
BK_LOGE(TAG, "audio: %d\n", audio);
}
if (strcmp(name, "token") == 0)
{
token = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
//BK_LOGE(TAG, "token: %s\n", token);
}
if (strcmp(name, "license") == 0)
{
license = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
//BK_LOGE(TAG, "token: %s\n", token);
}
inputs_list = inputs_list->next;
}
if (event_cb != NULL)
event_cb->event_remote_video(audio,license, token);
}
}
else if (strcmp(functionId->valuestring, EVENT_REMOTE_VIDEO_CLOSE) == 0)
{
if (event_cb != NULL)
event_cb->event_remote_video_close();
}
else if (strcmp(functionId->valuestring, EVENT_UNLOCK_REMOTE) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs"); // clientlist 是使用 cjson对象
if (NULL != inputs)
{
char *uid = NULL;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "uid") == 0)
{
uid = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
//BK_LOGE(TAG, "token: %s\n", token);
}
inputs_list = inputs_list->next;
}
if (event_cb != NULL)
event_cb->event_unlock_remote(uid);
}
}
else if (strcmp(functionId->valuestring, EVENT_UNLOCK_REMOTE_SOS) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs"); // clientlist 是使用 cjson对象
char *uid = NULL;
if (NULL != inputs)
{
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "uid") == 0)
{
uid = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
//BK_LOGE(TAG, "token: %s\n", token);
}
inputs_list = inputs_list->next;
}
}
if (event_cb != NULL)
event_cb->event_unlock_remote_sos(uid);
}
else if (strcmp(functionId->valuestring, EVENT_LOCK_REMOTE) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs"); // clientlist 是使用 cjson对象
char *uid = NULL;
if (NULL != inputs)
{
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "uid") == 0)
{
uid = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
//BK_LOGE(TAG, "token: %s\n", token);
}
inputs_list = inputs_list->next;
}
}
if (event_cb != NULL)
event_cb->event_lock_remote(uid);
}
else if (strcmp(functionId->valuestring, EVENT_HEARBEAT) == 0)
{
if (event_cb != NULL)
event_cb->event_hearbeat();
}
else if (strcmp(functionId->valuestring, FUNS_TAKE_PHOTO) == 0)
{
if (event_cb != NULL && event_cb->funs_take_photo != NULL)
event_cb->funs_take_photo();
}
else if (strcmp(functionId->valuestring, EVENT_DATA_TRANSFER) == 0)
{
data_transfer_t transfer ;
transfer.messageid = 12345678;
transfer.data_len = 0;
os_memset(transfer.data_value,0,sizeof(transfer.data_value));
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs");
if (NULL != inputs)
{
char *data = 0;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "data") == 0)
{
data = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
os_strcpy(transfer.data_value,data);
transfer.data_len = strlen(transfer.data_value);
}
inputs_list = inputs_list->next;
}
}
if (event_cb != NULL)
event_cb->event_data_transfer(&transfer);
}
else if (strcmp(functionId->valuestring, EVENT_AGORA_LINCENSE) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs");
if (NULL != inputs)
{
char *data = 0;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "license") == 0)
{
data = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
if (event_cb != NULL)
event_cb->event_agora_license(data, strlen(data));
}
inputs_list = inputs_list->next;
}
}
}
else if (strcmp(functionId->valuestring, EVENT_SET_WIFI) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs");
if (NULL != inputs)
{
char *ssid = 0;
char *pwd = 0;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "ssid") == 0)
{
ssid = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
BK_LOGE(TAG,"\nEVENT_SET_WIFI:%s \n",ssid);
}
if (strcmp(name, "password") == 0)
{
pwd = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
BK_LOGE(TAG,"\nEVENT_SET_WIFI:%s \n",pwd);
}
inputs_list = inputs_list->next;
}
if(ssid == NULL || strlen(ssid) == 0){
if (event_cb != NULL)
event_cb->event_set_wifi("", 0,"", 0);
return;
}
if (event_cb != NULL)
event_cb->event_set_wifi(ssid, strlen(ssid),pwd, strlen(pwd));
}
}
else if (strcmp(functionId->valuestring, EVENT_VIDEO_QUALITY) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs");
if (NULL != inputs)
{
int video_quality = 0;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "video_quality") == 0)
{
video_quality = cJSON_GetObjectItem(inputs_list, "value")->valueint;
BK_LOGE(TAG, "video_quality: %d\n", video_quality);
if (event_cb != NULL)
event_cb->event_set_video_quality(video_quality);
}
inputs_list = inputs_list->next;
}
}
}
else if (strcmp(functionId->valuestring, FUNS_OTA) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs");
if (NULL != inputs)
{
int device_type = 0;
char *data = 0;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "device_type") == 0)
{
char * str_device_type = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
if(str_device_type !=NULL && strlen(str_device_type)>0){
device_type = atoi(str_device_type);
BK_LOGE(TAG, "#device_type: %d\n", device_type);
}
BK_LOGE(TAG, "@device_type: %d\n", device_type);
}
if (strcmp(name, "ota_file_url") == 0)
{
data = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
BK_LOGE(TAG, "ota_file_url: %s\n", data);
}
inputs_list = inputs_list->next;
}
if (event_cb != NULL && event_cb->funs_ota != NULL)
event_cb->funs_ota(device_type,data);
}
}
else if (strcmp(functionId->valuestring, EVENT_LOCK_OTA_HTTP) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs");
if (NULL != inputs)
{
char *data = 0;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "file_url") == 0)
{
data = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
if (event_cb != NULL)
event_cb->event_ota_http(data);
}
inputs_list = inputs_list->next;
}
}
}
else if (strcmp(functionId->valuestring, EVENT_LOCK_OTA_HTTPS) == 0)
{
cJSON *inputs = cJSON_GetObjectItem(json_payload, "inputs");
if (NULL != inputs)
{
char *data = 0;
cJSON *inputs_list = inputs->child;
while (inputs_list != NULL)
{
char *name = cJSON_GetObjectItem(inputs_list, "name")->valuestring;
if (strcmp(name, "file_url") == 0)
{
data = cJSON_GetObjectItem(inputs_list, "value")->valuestring;
if (event_cb != NULL)
event_cb->event_ota_https(data);
}
inputs_list = inputs_list->next;
}
}
}
//poka_client_replay_invoke(PRODUCTID,ilock_device_id,messageId->valuestring,FUN_TRUE);
//cJSON_free(json_payload);
cJSON_Delete(json_payload);
#endif
}
void mqtt_subscribe_read_callback(char *topic, int topic_len, void *payload, int payload_len, void *ctx){
//BK_LOGE(TAG,"subscribe_callback:%s",topic);
//BK_LOGE(TAG,"subscribe_callback:%s",payload);
//BK_LOGE(TAG,"mqtt_subscribe_callback\n");
cJSON* json_payload = cJSON_Parse(payload);
//cJSON* headers = cJSON_GetObjectItem(json_payload,"headers");
cJSON* properties = cJSON_GetObjectItem(json_payload,"properties");
int sizes = cJSON_GetArraySize(properties);
for(int i = 0 ; i < sizes ;i++){
cJSON* item = cJSON_GetArrayItem(properties, i);
BK_LOGE(TAG,"\nread properties:%s\n",item->valuestring);
}
if(sizes > 0){
cJSON* messageId = cJSON_GetObjectItem(json_payload,"messageId");
poka_client_reply_properties(messageId->valuestring);
}
//poka_client_replay_invoke(messageId->valuestring,FUN_TRUE);
cJSON_Delete(json_payload);
}
int poka_client_gen_username(char* username,int username_len,char* secureId, char* time_t){
int ret = os_snprintf(username,
username_len,"%s|%s",
secureId,
time_t);
return ret;
}
int poka_client_gen_password(char* password,int password_len,char* secureId, char* secureKey, char* time_t){
char t_pwd[64] = {0};
int ret = os_snprintf(t_pwd,
sizeof(t_pwd), "%s|%s|%s",
secureId,
time_t,
secureKey);
// BK_LOGE(TAG,"t_pwd:%s\n",t_pwd);
int i;
char temp[8] = {0};
// unsigned char digest[16]; //存放结果
// char hexbuf[128]="12334567";
unsigned char decrypt[16] = {0};
unsigned char decrypt32[64] = {0};
MD5_CTX md5c;
MMD5Init(&md5c); // 初始化
MMD5Update(&md5c, (unsigned char *)t_pwd, strlen(t_pwd));
MMD5Final(&md5c, decrypt);
strcpy((char *)decrypt32, "");
for (i = 0; i < 16; i++)
{
sprintf(temp, "%02x", decrypt[i]);
strcat((char *)decrypt32, temp);
strcat((char *)password, temp);
}
//BK_LOGE(TAG, "md5:%s\n", decrypt32);
return ret;
}
void poka_client_subscribe_topic(char *product, char* deviceId){
//读取设备属性: /{productId}/{deviceId}/properties/read
char topic_read[128] ={0};
sprintf(topic_read,"/%s/%s/properties/read",product,deviceId);
//修改设备属性: /{productId}/{deviceId}/properties/write
char topic_write[128] ={0};
sprintf(topic_write,"/%s/%s/properties/write",product,deviceId);
//调用设备功能: /{productId}/{deviceId}/function/invoke
char topic_invoke[128] ={0};
sprintf(topic_invoke,"/%s/%s/function/invoke",product,deviceId);
//BK_LOGE(TAG,"subscribe :%s\n",topic_read);
mqtt_subscribe(topic_read, &mqtt_subscribe_read_callback, NULL);
//BK_LOGE(TAG,"subscribe :%s\n",topic_write);
mqtt_subscribe(topic_write, &mqtt_subscribe_callback, NULL);
//BK_LOGE(TAG,"subscribe :%s\n",topic_invoke);
mqtt_subscribe(topic_invoke, &mqtt_subscribe_invoke_callback, NULL);
}
void poka_client_unsubscribe_topic(char *product, char* deviceId){
//读取设备属性: /{productId}/{deviceId}/properties/read
char topic_read[128] ={0};
sprintf(topic_read,"/%s/%s/properties/read",product,deviceId);
//修改设备属性: /{productId}/{deviceId}/properties/write
char topic_write[128] ={0};
sprintf(topic_write,"/%s/%s/properties/write",product,deviceId);
//调用设备功能: /{productId}/{deviceId}/function/invoke
char topic_invoke[128] ={0};
sprintf(topic_invoke,"/%s/%s/function/invoke",product,deviceId);
BK_LOGE(TAG,"unsubscribe :%s\n",topic_read);
mqtt_unsubscribe(topic_read);
BK_LOGE(TAG,"unsubscribe :%s\n",topic_write);
mqtt_unsubscribe(topic_write);
BK_LOGE(TAG,"unsubscribe :%s\n",topic_invoke);
mqtt_unsubscribe(topic_invoke);
}
void mqtt_update_connectinfo(){
char timestamp[32] = {0};
long long ltimestamp = app_time_timestamp_ms();
sprintf(timestamp,"%lld",(long long )ltimestamp); //s---> ms
BK_LOGE(TAG,"timestamp:%s\n",timestamp);
memset(mqtt_username,0,sizeof(mqtt_username));
poka_client_gen_username(mqtt_username,sizeof(mqtt_username),SECUREID, timestamp);
BK_LOGE(TAG,"\r\nupdate mqtt_username: %s\n", mqtt_username);
memset(mqtt_password,0,sizeof(mqtt_password));
poka_client_gen_password(mqtt_password,sizeof(mqtt_password),SECUREID, SECUREKEY,timestamp);
BK_LOGE(TAG,"\r\nupdate mqtt_password: %s\n", mqtt_username);
iotx_mqtt_param_t mqtt_params;
/* Initialize MQTT parameter */
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.client_id = ilock_device_id;
mqtt_params.username = mqtt_username;
mqtt_params.password = mqtt_password;
//IOT_MQTT_Update_mqtt_param(mqtt_get_instance(),&mqtt_params);
}
void mqtt_event_callback(int event, void *ctx){
//BK_LOGE(TAG,"mqtt_event_callback event:%d\n",event);
if(event == MQTT_INSTANCE_EVENT_DISCONNECTED){
BK_LOGE(TAG,"mqtt disconnect! \n");
//mqtt_update_connectinfo();
//poka_client_unsubscribe_topic(PRODUCTID,ilock_device_id);
//mqtt_deinit_instance();
//poka_client_init();
if(ble_boarding_get_state() == BOARDING_STATE_SCAN || ble_boarding_get_state() == BOARDING_STATE_SETWIFI){
}else{
//BK_LOGE(TAG,"MQTT ERROR REBOOT! \n");
//bk_reboot();
}
}
else if(event == MQTT_INSTANCE_EVENT_CONNECTED){
BK_LOGE(TAG,"mqtt connected!\n");
if (event_cb != NULL)
event_cb->event_link_state(event);
}
else if(event == MQTT_INSTANCE_EVENT_PING_RESP){
BK_LOGE(TAG,"mqtt recv ping respone\n");
//复位定时器
rtos_reload_timer(&timer_reboot_handle);
}
}
void poka_client_set_callback(poka_client_cb_ops_t *cb_ops){
event_cb = cb_ops;
}
static void reboot_timer_handle( void *arg )
{
BK_LOGE(TAG,"mqtt recv ping respone timeout--> REBOOT! \n");
bk_reboot();
}
void poka_stop_reboot_timer()
{
rtos_stop_timer(&timer_reboot_handle);
}
void poka_client_init(void){
//ntp_sync_to_rtc();
app_time_init();
char timestamp[32] = {0};
long long ltimestamp = app_time_timestamp_ms();
sprintf(timestamp,"%lld",(long long )ltimestamp); //s---> ms
BK_LOGE(TAG,"timestamp:%s\n",timestamp);
memset(mqtt_clientid,0,sizeof(mqtt_clientid));
poka_client_gen_username(mqtt_username,sizeof(mqtt_username),SECUREID, timestamp);
BK_LOGE(TAG,"\r\nmqtt_username: %s\n", mqtt_username);
poka_client_gen_password(mqtt_password,sizeof(mqtt_password),SECUREID, SECUREKEY,timestamp);
BK_LOGE(TAG,"\r\nmqtt_password: %s\n", mqtt_username);
mqtt_set_event_cb(&mqtt_event_callback, NULL);
mqtt_init_instance_poka(TMQTT_SERVER,TMQTT_PORT,ilock_device_id, mqtt_username, mqtt_password, 1024*4);
//mqtt_init_instance_poka("mqtts.heclouds.com",1883,"867698041071583",
// "YCx4b0m6Uv", "version=2018-10-31&res=products%2FYCx4b0m6Uv%2Fdevices%2F867698041071583&et=1745719251&method=sha1&sign=Qd56NvGRqQDtxUTciKKE9J%2BZvZY%3D", 1024*4);
poka_client_subscribe_topic(PRODUCTID,ilock_device_id);
//启动网络检测定时器,如果两分钟 没有收到PING 回应自动重启
rtos_init_timer(&timer_reboot_handle, ILOCK_MQTT_PINGPONG_TIMEOUT*1000, reboot_timer_handle, 0);
rtos_start_timer(&timer_reboot_handle);
}
void poka_client_reconnect(){
//poka_client_unsubscribe_topic(PRODUCTID,ilock_device_id);
mqtt_deinit_instance();
poka_client_init();
}
void poka_client_deinit(void){
mqtt_deinit_instance();
}