|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making IoT Hub available. |
| 3 | + * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | +
|
| 5 | + * Licensed under the MIT License (the "License"); you may not use this file except in |
| 6 | + * compliance with the License. You may obtain a copy of the License at |
| 7 | + * http://opensource.org/licenses/MIT |
| 8 | +
|
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 | + * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 11 | + * either express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdlib.h> |
| 18 | +#include <string.h> |
| 19 | + |
| 20 | +#include "qcloud_iot_import.h" |
| 21 | +#include "qcloud_iot_export.h" |
| 22 | + |
| 23 | +#include "utils_param_check.h" |
| 24 | + |
| 25 | +/* Enable this macro (also control by cmake) to use static string buffer to store device info */ |
| 26 | +/* To use specific storing methods like files/flash, disable this macro and implement dedicated methods */ |
| 27 | +#define DEBUG_DEV_INFO_USED |
| 28 | + |
| 29 | +#ifdef DEBUG_DEV_INFO_USED |
| 30 | +/* product Id */ |
| 31 | +static char sg_product_id[MAX_SIZE_OF_PRODUCT_ID + 1] = "PRODUCT_ID"; |
| 32 | + |
| 33 | +/* device name */ |
| 34 | +static char sg_device_name[MAX_SIZE_OF_DEVICE_NAME + 1] = "YOUR_DEV_NAME"; |
| 35 | + |
| 36 | +#ifdef DEV_DYN_REG_ENABLED |
| 37 | +/* product secret for device dynamic Registration */ |
| 38 | +static char sg_product_secret[MAX_SIZE_OF_PRODUCT_SECRET + 1] = "YOUR_PRODUCT_SECRET"; |
| 39 | +#endif |
| 40 | + |
| 41 | +#ifdef AUTH_MODE_CERT |
| 42 | +/* public cert file name of certificate device */ |
| 43 | +static char sg_device_cert_file_name[MAX_SIZE_OF_DEVICE_CERT_FILE_NAME + 1] = "YOUR_DEVICE_NAME_cert.crt"; |
| 44 | +/* private key file name of certificate device */ |
| 45 | +static char sg_device_privatekey_file_name[MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME + 1] = "YOUR_DEVICE_NAME_private.key"; |
| 46 | +#else |
| 47 | +/* device secret of PSK device */ |
| 48 | +static char sg_device_secret[MAX_SIZE_OF_DEVICE_SECRET + 1] = "YOUR_IOT_PSK"; |
| 49 | +#endif |
| 50 | + |
| 51 | +#ifdef GATEWAY_ENABLED |
| 52 | +/* sub-device product id */ |
| 53 | +static char sg_sub_device_product_id[MAX_SIZE_OF_PRODUCT_ID + 1] = "PRODUCT_ID"; |
| 54 | +/* sub-device device name */ |
| 55 | +static char sg_sub_device_name[MAX_SIZE_OF_DEVICE_NAME + 1] = "YOUR_SUB_DEV_NAME"; |
| 56 | +#endif |
| 57 | + |
| 58 | +static int device_info_copy(void *pdst, void *psrc, uint8_t max_len) |
| 59 | +{ |
| 60 | + if(strlen(psrc) > max_len){ |
| 61 | + return QCLOUD_ERR_FAILURE; |
| 62 | + } |
| 63 | + memset(pdst, '\0', max_len); |
| 64 | + strncpy(pdst, psrc, max_len); |
| 65 | + return QCLOUD_RET_SUCCESS; |
| 66 | +} |
| 67 | + |
| 68 | +#endif |
| 69 | + |
| 70 | +int HAL_SetDevInfo(void *pdevInfo) |
| 71 | +{ |
| 72 | + POINTER_SANITY_CHECK(pdevInfo, QCLOUD_ERR_DEV_INFO); |
| 73 | + int ret; |
| 74 | + DeviceInfo *devInfo = (DeviceInfo *)pdevInfo; |
| 75 | + |
| 76 | +#ifdef DEBUG_DEV_INFO_USED |
| 77 | + ret = device_info_copy(sg_product_id, devInfo->product_id, MAX_SIZE_OF_PRODUCT_ID);//set product ID |
| 78 | + ret |= device_info_copy(sg_device_name, devInfo->device_name, MAX_SIZE_OF_DEVICE_NAME);//set dev name |
| 79 | + |
| 80 | +#ifdef AUTH_MODE_CERT |
| 81 | + ret |= device_info_copy(sg_device_cert_file_name, devInfo->dev_cert_file_name, MAX_SIZE_OF_DEVICE_CERT_FILE_NAME);//set dev cert file name |
| 82 | + ret |= device_info_copy(sg_device_privatekey_file_name, devInfo->dev_key_file_name, MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME);//set dev key file name |
| 83 | +#else |
| 84 | + ret |= device_info_copy(sg_device_secret, devInfo->device_secret, MAX_SIZE_OF_DEVICE_SECRET);//set dev secret |
| 85 | +#endif |
| 86 | + |
| 87 | +#else |
| 88 | + Log_e("HAL_SetDevInfo not implement yet"); |
| 89 | + ret = QCLOUD_ERR_DEV_INFO; |
| 90 | +#endif |
| 91 | + |
| 92 | + if(QCLOUD_RET_SUCCESS != ret) { |
| 93 | + Log_e("Set device info err"); |
| 94 | + ret = QCLOUD_ERR_DEV_INFO; |
| 95 | + } |
| 96 | + return ret; |
| 97 | +} |
| 98 | + |
| 99 | +int HAL_GetDevInfo(void *pdevInfo) |
| 100 | +{ |
| 101 | + POINTER_SANITY_CHECK(pdevInfo, QCLOUD_ERR_DEV_INFO); |
| 102 | + int ret; |
| 103 | + DeviceInfo *devInfo = (DeviceInfo *)pdevInfo; |
| 104 | + memset((char *)devInfo, '\0', sizeof(DeviceInfo)); |
| 105 | + |
| 106 | +#ifdef DEBUG_DEV_INFO_USED |
| 107 | + ret = device_info_copy(devInfo->product_id, sg_product_id, MAX_SIZE_OF_PRODUCT_ID);//get product ID |
| 108 | + ret |= device_info_copy(devInfo->device_name, sg_device_name, MAX_SIZE_OF_DEVICE_NAME);//get dev name |
| 109 | + |
| 110 | +#ifdef DEV_DYN_REG_ENABLED |
| 111 | + ret |= device_info_copy(devInfo->product_secret, sg_product_secret, MAX_SIZE_OF_PRODUCT_SECRET );//get product ID |
| 112 | +#endif |
| 113 | + |
| 114 | +#ifdef AUTH_MODE_CERT |
| 115 | + ret |= device_info_copy(devInfo->dev_cert_file_name, sg_device_cert_file_name, MAX_SIZE_OF_DEVICE_CERT_FILE_NAME);//get dev cert file name |
| 116 | + ret |= device_info_copy(devInfo->dev_key_file_name, sg_device_privatekey_file_name, MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME);//get dev key file name |
| 117 | +#else |
| 118 | + ret |= device_info_copy(devInfo->device_secret, sg_device_secret, MAX_SIZE_OF_DEVICE_SECRET);//get dev secret |
| 119 | +#endif |
| 120 | + |
| 121 | +#else |
| 122 | + Log_e("HAL_GetDevInfo not implement yet"); |
| 123 | + ret = QCLOUD_ERR_DEV_INFO; |
| 124 | +#endif |
| 125 | + |
| 126 | + if(QCLOUD_RET_SUCCESS != ret){ |
| 127 | + Log_e("Get device info err"); |
| 128 | + ret = QCLOUD_ERR_DEV_INFO; |
| 129 | + } |
| 130 | + return ret; |
| 131 | +} |
| 132 | + |
| 133 | +#ifdef GATEWAY_ENABLED |
| 134 | +int HAL_GetGwDevInfo(void *pgwDeviceInfo) |
| 135 | +{ |
| 136 | + POINTER_SANITY_CHECK(pgwDeviceInfo, QCLOUD_ERR_DEV_INFO); |
| 137 | + int ret; |
| 138 | + GatewayDeviceInfo *gwDevInfo = (GatewayDeviceInfo *)pgwDeviceInfo; |
| 139 | + memset((char *)gwDevInfo, 0, sizeof(GatewayDeviceInfo)); |
| 140 | + |
| 141 | +#ifdef DEBUG_DEV_INFO_USED |
| 142 | + ret = HAL_GetDevInfo(&(gwDevInfo->gw_info));//get gw dev info |
| 143 | + //only one sub-device is supported now |
| 144 | + gwDevInfo->sub_dev_num = 1; |
| 145 | + gwDevInfo->sub_dev_info = (DeviceInfo *)HAL_Malloc(sizeof(DeviceInfo)*(gwDevInfo->sub_dev_num)); |
| 146 | + memset((char *)gwDevInfo->sub_dev_info, '\0', sizeof(DeviceInfo)); |
| 147 | + //copy sub dev info |
| 148 | + ret = device_info_copy(gwDevInfo->sub_dev_info->product_id, sg_sub_device_product_id, MAX_SIZE_OF_PRODUCT_ID); |
| 149 | + ret |= device_info_copy(gwDevInfo->sub_dev_info->device_name, sg_sub_device_name, MAX_SIZE_OF_DEVICE_NAME); |
| 150 | + |
| 151 | +#else |
| 152 | + Log_e("HAL_GetDevInfo not implement yet"); |
| 153 | + ret = QCLOUD_ERR_DEV_INFO; |
| 154 | +#endif |
| 155 | + |
| 156 | + if(QCLOUD_RET_SUCCESS != ret) { |
| 157 | + Log_e("Get gateway device info err"); |
| 158 | + ret = QCLOUD_ERR_DEV_INFO; |
| 159 | + } |
| 160 | + return ret; |
| 161 | +} |
| 162 | +#endif |
0 commit comments