diff --git a/src/Framework/crypto/crypto.c b/src/Framework/crypto/crypto.c index f5b9b40..3a4f270 100644 --- a/src/Framework/crypto/crypto.c +++ b/src/Framework/crypto/crypto.c @@ -30,7 +30,7 @@ const char* rand_string(char* pRandKey, int ilen) return (const char*)pRandKey; } -const char* hash_sha1(unsigned char* pData, int iSize, char* pHashCode) +const char* hash_sha1(unsigned char* pData, int iSize, char pHashCode[SHA1_STR_LEN]) { int len; unsigned char sha1[SHA1_DIGEST_LENGTH]; @@ -46,8 +46,89 @@ const char* hash_sha1(unsigned char* pData, int iSize, char* pHashCode) EVP_DigestFinal_ex(&evpCtx, sha1, &len); EVP_MD_CTX_cleanup(&evpCtx); - print_hex_dump_bytes("SHA1", DUMP_PREFIX_OFFSET, sha1, SHA1_DIGEST_LENGTH); + //print_hex_dump_bytes("SHA1", DUMP_PREFIX_OFFSET, sha1, SHA1_DIGEST_LENGTH); return (const char*)IHW_bin2hex(pHashCode, sha1, len); } +const char* hash_md5(const unsigned char* pBuf, int iBufLen, char pHashCode[MD5_STR_LEN]) +{ + int size = 0; + EVP_MD_CTX ctx; + unsigned char md5[EVP_MAX_MD_SIZE]; + + if(pBuf == NULL || iBufLen <= 0) + { + return NULL; + } + + memset(md5, 0, EVP_MAX_MD_SIZE); + EVP_MD_CTX_init(&ctx); + EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); + EVP_DigestUpdate(&ctx, pBuf, iBufLen); + EVP_DigestFinal_ex(&ctx, md5, &iBufLen); + EVP_MD_CTX_cleanup(&ctx); + + return (const char*)IHW_bin2hex(pHashCode, md5, iBufLen); +} + +const char* base64_encode(unsigned char* pSrc, int sLen, char** pEncode) +{ + int size; + + if(pSrc == NULL || sLen <= 0) + { + return (NULL); + } + + size = ((sLen / 3) * 4) + 4 + (sLen / 64) + sLen % 64; + + *pEncode = (char*)malloc(size); + + if(*pEncode == NULL) + { + LOG_EX(LOG_Debug, "Malloc Memory Error: %d\n", size); + return NULL; + } + + memset(*pEncode, 0, size); + + EVP_EncodeBlock(*pEncode, (const unsigned char *)pSrc, sLen); + +// fprintf(stdout, "Src: \n[%s]\n", pSrc); +// fprintf(stdout, "Base64(%d --> %d | %d) Bytes: \n[%s]\n", sLen, size, strlen(pEncode), pEncode); + + return (const char*)*pEncode; +} + +unsigned char* base64_decode(const char *pBase64, unsigned char** pDecode, int* pOutSize) +{ + int size = 0; + + if(pBase64 == NULL || strlen(pBase64) == 0) + { + return (NULL); + } + + size = strlen(pBase64); + + *pDecode = (char*)malloc(size); + + if(*pDecode == NULL) + { + LOG_EX(LOG_Debug, "Malloc Memory Error: %d\n", size); + return NULL; + } + + memset(*pDecode, 0, size); + + size = EVP_DecodeBlock(*pDecode, (const unsigned char *)pBase64, size); + + if(pOutSize) + { + *pOutSize = size; + } + + return (*pDecode); +} + diff --git a/src/Framework/hal/hal_mtk.c b/src/Framework/hal/hal_mtk.c index bcfaff0..be47074 100644 --- a/src/Framework/hal/hal_mtk.c +++ b/src/Framework/hal/hal_mtk.c @@ -1,27 +1,224 @@ #include +#include #include +#include "protocol.h" #include "hal_mtk.h" #include "log.h" -#define CFG_SAVE_PATH ("/var/mqttcfg/mqtt_cfg.cfg") +#define CFG_SAVE_PATH ("/var/mqtt_cfg.cfg") + +static char* g_pDeviceId = NULL; +static char* g_pDeviceSecret = NULL; +static char* g_pDeviceName = NULL; +static char* g_pIoTId = NULL; + +#define SAFE_RESET_STRING(src, val) \ + do { \ + if(src) free(src); \ + if(val) src = strdup(val); \ + } while (0) + +int hal_save_device_register_info(unsigned char* pRegInfo, int iSize) +{ + FILE* fp = fopen(CFG_SAVE_PATH, "w"); + + if(fp == NULL) + { + LOG_EX(LOG_Error, "Open File %s Error\n", CFG_SAVE_PATH); + return -ERR_OPEN_FILE; + } + + fwrite(pRegInfo, 1, iSize, fp); + fclose(fp); + + return 0; +} + +int hal_load_device_register_info(void) +{ + int fsize = 0; + unsigned char* pBuf = NULL; + FILE* fp = NULL; + + if(!is_file_exists(CFG_SAVE_PATH)) + { + return 0; + } + + fp = fopen(CFG_SAVE_PATH, "r"); + + if(fp == NULL) + { + LOG_EX(LOG_Error, "Open File %s Error\n", CFG_SAVE_PATH); + return -ERR_OPEN_FILE; + } + + GET_FILE_SIZE(CFG_SAVE_PATH, fsize); + + pBuf = (unsigned char*)malloc(fsize + 1); + + if(pBuf == NULL) + { + LOG_EX(LOG_Error, "Malloc Memory Error: %d\n", fsize); + fclose(fp); + return -ERR_MALLOC_MEMORY; + } + + pBuf[fsize] = 0; + + fread(pBuf, 1, fsize, fp); + fclose(fp); + + hal_get_regist_info((const char*)pBuf); + + free(pBuf); + + return 0; +} + + +int hal_get_regist_info(const char* pMsg) +{ + int err = 0; + PREGIST_DEVICE pRegInfo = (PREGIST_DEVICE)Json2Struct(pMsg, JE_REGDEVICE, CRYPTO_NONE, &err); + + if(pRegInfo == NULL || err != ERR_OK) + { + LOG_EX(LOG_Error, "Decode Message %s Error: %d\n", pMsg, err); + + if(pRegInfo) + { + free(pRegInfo); + } + + return -ERR_INPUT_PARAMS; + } + + if(pRegInfo->code == 200) + { + if(strcmp(pRegInfo->data.ProductKey, NETEASE_PRODUCT_KEY) != 0) + { + LOG_EX(LOG_Error, "Server Key: %s\nDevice Key: %s\n", pRegInfo->data.ProductKey, NETEASE_PRODUCT_KEY); + } +// else + { + SAFE_RESET_STRING(g_pDeviceSecret, pRegInfo->data.DeviceSecret); + SAFE_RESET_STRING(g_pDeviceName, pRegInfo->data.DeviceName); + SAFE_RESET_STRING(g_pIoTId, pRegInfo->data.IotId); + LOG_EX(LOG_Debug, "DeviceSecret: %s\n", g_pDeviceSecret); + LOG_EX(LOG_Debug, "DeviceName: %s\n", g_pDeviceName); + LOG_EX(LOG_Debug, "IoTId: %s\n", g_pIoTId); + } + } + else + { + LOG_EX(LOG_Error, "Server Return error(%d): %s\n", pRegInfo->code, pRegInfo->msg); + } + + free(pRegInfo); + + return 0; +} const char* hal_get_device_id(void) -{ +{ return "547e1a1d7a7e5ced38ee8a3989fb8f97"; } const char* hal_get_device_secret(void) { - return "266726e07bf3210192e0868d9f7315f7"; + return g_pDeviceSecret;//"266726e07bf3210192e0868d9f7315f7"; } const char* hal_get_device_name(void) { - return "e0d7930776e745508ef930e1b0efaf65"; + return g_pDeviceName;//"e0d7930776e745508ef930e1b0efaf65"; } const char* hal_get_device_iot_id(void) { - return "f65250e202664a229446b6bd2d6bccc4"; + return g_pIoTId;//"f65250e202664a229446b6bd2d6bccc4"; } + +int hal_device_name_to_id(void) +{ +#define MQTT_BROADCAST_MAX_GROUP 90 + if(g_pDeviceName) + { + int val = 0; + int len = strlen(g_pDeviceName); + for(int i = 0; i < len; i++) + { + val += g_pDeviceName[i]; + } + + val = (val % MQTT_BROADCAST_MAX_GROUP) + 1; + + return val; + } + else + { + return 0; + } +} + +int hal_is_device_registed(void) +{ + if(g_pDeviceSecret == NULL || g_pDeviceName == NULL || g_pIoTId == NULL) + { + return 0; + } + + if(strlen(g_pDeviceSecret) == 0 || strlen(g_pDeviceName) == 0 || strlen(g_pIoTId) == 0) + { + return 0; + } + + return 1; +} + +int hal_init(void) +{ + hal_load_device_register_info(); + + return 0; +} + +int hal_get_exec_message(const char *pCmd, char **pResult) +{ + FILE *pFile = NULL; + unsigned int uRdSize = 0; + char *pCmdOut; + + *pResult = NULL; + + if(pCmd == NULL || strlen(pCmd) == 0) + { + return (-ERR_INPUT_PARAMS); + } + + pFile = popen(pCmd, "r"); + + if(pFile == NULL) + { + return (-ERR_OPEN_FILE); + } + + *pResult = (char *)malloc(4096); + pCmdOut = *pResult; + + uRdSize = fread(pCmdOut, sizeof(char), 4096, pFile); + + pCmdOut[uRdSize] = 0; + + if(pCmdOut[strlen(pCmdOut) - 1] == '\n') + { + pCmdOut[strlen(pCmdOut) - 1] = 0; + } + + pclose(pFile); + //fprintf(stdout, "%s --> [%s](%u)\n", pCmd, pCmdOut, uRdSize); + return (0); +} + diff --git a/src/Framework/http/http.c b/src/Framework/http/http.c index 5b09a96..ba9e293 100644 --- a/src/Framework/http/http.c +++ b/src/Framework/http/http.c @@ -15,11 +15,14 @@ static size_t __writeDataCb(void *pData, size_t size, size_t nmemb, void *pParams) { + UT_string* pPrv = (UT_string*)pParams; + int iMemSize = size * nmemb; - - if(iMemSize > 0) + + if(iMemSize > 0 && pPrv) { - LOG_EX(LOG_Debug, "Receive: %s\n", (char*)pData); + utstring_bincpy(pPrv, pData, iMemSize); + //LOG_EX(LOG_Debug, "Receive: %s\n", (char*)pData); } return iMemSize; @@ -43,7 +46,8 @@ int device_register(void) const char* pPostMsg = ""; UT_string* pProduct, *pNonce, *pCurTime, *pChecksum; char randKey[HTTPS_RANDOM_LEN + 1]; - char checkSum[64]; + char checkSum[SHA1_STR_LEN + 1]; + UT_string* pResult = NULL; time_t timsStamp = time(NULL); pCurl = curl_easy_init(); @@ -51,10 +55,12 @@ int device_register(void) if(pCurl == NULL) { LOG_EX(LOG_Error, "Create CURL handle error\n"); - return -1; + return -ERR_CREATE_OBJECTS; } - memset(checkSum, 0, 64); + utstring_new(pResult); + + memset(checkSum, 0, SHA1_STR_LEN + 1); memset(randKey, 0, HTTPS_RANDOM_LEN + 1); rand_string(randKey, HTTPS_RANDOM_LEN); @@ -78,6 +84,7 @@ int device_register(void) curl_easy_setopt(pCurl, CURLOPT_URL, NETEASE_API_SERVER); curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, __writeDataCb); + curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pResult); curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(pCurl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pList); @@ -102,11 +109,16 @@ int device_register(void) { LOG_EX(LOG_Error, "Run Http POST error: %s\n", curl_easy_strerror(ret)); curl_easy_cleanup(pCurl); - return -2; + utstring_free(pResult); + return -ERR_NETWORK_SEND; } curl_easy_cleanup(pCurl); - LOG_EX(LOG_Debug, "HTTP Post Finished\n"); + LOG_EX(LOG_Debug, "HTTP Post Finished:\n%s\n", utstring_body(pResult)); + hal_save_device_register_info(utstring_body(pResult), utstring_len(pResult)); + hal_get_regist_info(utstring_body(pResult)); + utstring_free(pResult); + return 0; } diff --git a/src/Framework/mqtt/mqtt.c b/src/Framework/mqtt/mqtt.c index 7015f0d..605f9e9 100644 --- a/src/Framework/mqtt/mqtt.c +++ b/src/Framework/mqtt/mqtt.c @@ -14,6 +14,11 @@ static struct mosquitto *g_pMosq = NULL; static pthread_t g_mqttThread; static int g_isMqttConnected = FALSE; +static UT_string* g_pTopicGet; +static UT_string* g_pTopicBroadcase; +static UT_string* g_pShadow; +static UT_string* g_pUpdate; + static void __mqtt_connected_cb(struct mosquitto* pMosq, void* obj, int rc) { @@ -130,11 +135,16 @@ static int __mqtt_connect_init(void) { LOG_EX(LOG_Debug, "MQTT Connect OK\n"); } - - utstring_renew(pString); - utstring_printf(pString, "/%s/%s/get", NETEASE_PRODUCT_KEY, hal_get_device_name()); - mosquitto_subscribe(pMosq, NULL, utstring_body(pString), 1); + + utstring_printf(g_pTopicGet, "/%s/%s/get", NETEASE_PRODUCT_KEY, hal_get_device_name()); + mosquitto_subscribe(pMosq, NULL, utstring_body(g_pTopicGet), 1); + + utstring_printf(g_pTopicBroadcase, "/broadcast/%s/%d/get", NETEASE_PRODUCT_KEY, hal_device_name_to_id()); + mosquitto_subscribe(pMosq, NULL, utstring_body(g_pTopicBroadcase), 1); + + utstring_printf(g_pShadow, "/shadow/update/%s/%s", NETEASE_PRODUCT_KEY, hal_get_device_name()); + utstring_printf(g_pUpdate, "/%s/%s/update", NETEASE_PRODUCT_KEY, hal_get_device_name()); g_pMosq = pMosq; g_isMqttConnected = TRUE; @@ -164,5 +174,9 @@ static void* __mqtt_serverce_cb(void *p) void mqtt_proxy_setup(void) { + utstring_new(g_pTopicGet); + utstring_new(g_pTopicBroadcase); + utstring_new(g_pShadow); + utstring_new(g_pUpdate); pthread_create(&g_mqttThread, NULL, __mqtt_serverce_cb, NULL); } diff --git a/src/include/crypto.h b/src/include/crypto.h index e62b205..e4e3a7a 100644 --- a/src/include/crypto.h +++ b/src/include/crypto.h @@ -6,10 +6,14 @@ extern "C" { #endif #define SHA1_DIGEST_LENGTH (20) - +#define SHA1_STR_LEN (40) +#define MD5_STR_LEN (32) const char* rand_string(char* pRandKey, int ilen); -const char* hash_sha1(unsigned char* pData, int iSize, char* pHashCode); +const char* hash_sha1(unsigned char* pData, int iSize, char pHashCode[SHA1_STR_LEN]); +const char* hash_md5(const unsigned char* pBuf, int iBufLen, char pHashCode[MD5_STR_LEN]); +const char* base64_encode(unsigned char* pSrc, int sLen, char** pEncode); +unsigned char* base64_decode(const char *pBase64, unsigned char** pDecode, int* pOutSize); #ifdef __cplusplus } diff --git a/src/include/hal_mtk.h b/src/include/hal_mtk.h index 2e07767..efd7fb3 100644 --- a/src/include/hal_mtk.h +++ b/src/include/hal_mtk.h @@ -21,7 +21,13 @@ extern "C" { const char* hal_get_device_id(void); const char* hal_get_device_iot_id(void); const char* hal_get_device_name(void); +int hal_device_name_to_id(void); const char* hal_get_device_secret(void); +int hal_save_device_register_info(unsigned char* pRegInfo, int iSize); +int hal_get_regist_info(const char* pMsg); +int hal_init(void); +int hal_is_device_registed(void); +int hal_get_exec_message(const char *pCmd, char **pResult); #ifdef __cplusplus } diff --git a/src/include/http.h b/src/include/http.h index b55b537..fdc2b85 100644 --- a/src/include/http.h +++ b/src/include/http.h @@ -5,6 +5,7 @@ extern "C" { #endif +int device_register(void); #ifdef __cplusplus } diff --git a/src/include/log.h b/src/include/log.h index 5e826a7..d4411d2 100644 --- a/src/include/log.h +++ b/src/include/log.h @@ -13,6 +13,7 @@ extern "C" { #ifndef __KERNEL__ #include #include +#include #else #include #endif @@ -98,17 +99,7 @@ typedef enum { ERR_OK = 0, ERR_INPUT_PARAMS, ERR_NO_ITEMS, - ERR_GET_BUS, - ERR_DBUS_CONNECTION, - ERR_REQUEST_BUS_NAME, - ERR_SET_WATCH_FUNCTION, - ERR_SET_TIMEOUT_FUNCTION, - ERR_BUS_MATCH, - ERR_BUS_SET_MSG_CB, - ERR_DBUS_CREATE_MSG, - ERR_BUS_SEND_MSG, - ERR_DBUS_MSG_TO_LARGE, - ERR_BUS_RCV_MSG, + ERR_CREATE_OBJECTS, ERR_ADD_TASK, ERR_UNSUP_EVP_TYPE, @@ -266,6 +257,20 @@ const char* format_hex_buf(const char* prefix_str, int prefix_type, void print_hex_dump_bytes(const char* prefix_str, int prefix_type, const void* buf, int len); #endif + +int is_file_exists(const char* pPath); + +#define GET_FILE_SIZE(path, size) \ + do { \ + struct stat st; \ + memset(&st, 0, sizeof(struct stat)); \ + if (stat(path, &st) != 0) { \ + size = -1; \ + } else { \ + size = st.st_size; \ + } \ + } while (0) + #ifdef __cplusplus } #endif diff --git a/src/log/log.c b/src/log/log.c index ae52c3a..7306b7f 100644 --- a/src/log/log.c +++ b/src/log/log.c @@ -39,17 +39,6 @@ #define MAX_LOG_FILE_SIZE (1024 * 1024) #define LOG_PRE_SIZE (512) -#define GET_FILE_SIZE(path, size) \ - do { \ - struct stat st; \ - memset(&st, 0, sizeof(struct stat)); \ - if (stat(path, &st) != 0) { \ - size = -1; \ - } else { \ - size = st.st_size; \ - } \ - } while (0) - typedef struct LOG_ITEM { LOG_LEVEL level; @@ -95,6 +84,20 @@ static int g_logSock = -1; static int g_iMinLevel = LOG_Fatal | LOG_Error | LOG_Warn | LOG_Debug | LOG_Info | LOG_Step; +int is_file_exists(const char* pPath) +{ + struct stat st; + + if (stat(pPath, &st) != 0) + { + return FALSE; + } + else + { + return TRUE; + } +} + /** * @brief Log 调试等级转字符串 * @param level 调试等级 diff --git a/src/main.c b/src/main.c index 2b07c2e..7106ece 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,7 @@ #include "uthash/utstring.h" +#include "hal_mtk.h" #include "mqtt.h" #include "http.h" #include "log.h" @@ -63,7 +64,7 @@ int main(int argc, char* argv[]) int rc; int major, minor, rev; struct mosquitto *pMosq = NULL; - + IHW_InitLOG("MQTT", NULL, TRUE); IHW_EnableLogLevel(LOG_Fatal | LOG_Error | LOG_Warn | LOG_Debug | LOG_Info, 1); IHW_RunLogService(); @@ -72,11 +73,19 @@ int main(int argc, char* argv[]) curl_global_init(CURL_GLOBAL_ALL); mosquitto_lib_init(); - + + hal_init(); //evp_sha1(); //curl_init(); //device_register(); //mqtt_init(); + + + if(!hal_is_device_registed()) + { + device_register(); + } + mqtt_proxy_setup(); while(TRUE)