linkkit_sample.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /** USER NOTIFICATION
  2. * this sample code is only used for evaluation or test of the iLop project.
  3. * Users should modify this sample code freely according to the product/device TSL, like
  4. * property/event/service identifiers, and the item value type(type, length, etc...).
  5. * Create user's own execution logic for specific products.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <stdarg.h>
  12. #if !(WIN32)
  13. #include <unistd.h>
  14. #endif
  15. #include "linkkit_export.h"
  16. #include "iot_import.h"
  17. /*
  18. * example for product "鐏�-Demo"
  19. */
  20. #ifdef ON_DAILY
  21. #define DM_PRODUCT_KEY_1 "a1nmfrdo3MI"
  22. #define DM_DEVICE_NAME_1 "light_demo_for_ilop_device_test"
  23. #define DM_DEVICE_SECRET_1 "kobN5zg08IwlgbqSUeaxo0vbEsOiEI7b"
  24. #else
  25. #define DM_PRODUCT_KEY_1 "a1grYGVCPWl"
  26. #define DM_DEVICE_NAME_1 "0402_09"
  27. #define DM_DEVICE_SECRET_1 "DocxiD5wS6x9HNZrbC2CAoi9UnYP74Xt"
  28. #endif
  29. #define DM_DEVICE_ID_1 "IoTxHttpTestDev_001"
  30. #define LINKKIT_PRINTF(...) \
  31. do { \
  32. printf("\e[0;32m%s@line%d\t:", __FUNCTION__, __LINE__); \
  33. printf(__VA_ARGS__); \
  34. printf("\e[0m"); \
  35. } while (0)
  36. /* identifier of property/service/event, users should modify this macros according to your own product TSL. */
  37. #define EVENT_PROPERTY_POST_IDENTIFIER "post"
  38. #define EVENT_ERROR_IDENTIFIER "Error"
  39. #define EVENT_ERROR_OUTPUT_INFO_IDENTIFIER "ErrorCode"
  40. #define EVENT_CUSTOM_IDENTIFIER "Custom"
  41. /* specify ota buffer size for ota service, ota service will use this buffer for bin download. */
  42. #define OTA_BUFFER_SIZE (512+1)
  43. /* PLEASE set RIGHT tsl string according to your product. */
  44. #if !(WIN32)
  45. const char TSL_STRING[] = "{\"schema\":\"http://aliyun/iot/thing/desc/schema\",\"profile\":{\"productKey\":\"a1saiXev7Q9\",\"deviceName\":\"q0LP4t1AWGnbp3uwF6Md\"},\"services\":[{\"outputData\":[],\"identifier\":\"set\",\"inputData\":[{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\"},{\"identifier\":\"RGBColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Red\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"红色\"},{\"identifier\":\"Green\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"绿色\"},{\"identifier\":\"Blue\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"蓝色\"}],\"type\":\"struct\"},\"name\":\"RGB调色\"},{\"identifier\":\"NightLightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"夜灯开关\"},{\"identifier\":\"WorkMode\",\"dataType\":{\"specs\":{\"0\":\"手动\",\"1\":\"阅读\",\"2\":\"影院\",\"3\":\"夜灯\",\"4\":\"生活\",\"5\":\"柔和\"},\"type\":\"enum\"},\"name\":\"工作模式\"},{\"identifier\":\"ColorTemperature\",\"dataType\":{\"specs\":{\"unit\":\"K\",\"min\":\"2700\",\"unitName\":\"开尔文\",\"max\":\"6500\"},\"type\":\"int\"},\"name\":\"冷暖色温\"},{\"identifier\":\"Brightness\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"明暗度\"},{\"identifier\":\"HSLColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Hue\",\"dataType\":{\"specs\":{\"unit\":\"°\",\"min\":\"0\",\"unitName\":\"度\",\"max\":\"360\"},\"type\":\"int\"},\"name\":\"色调\"},{\"identifier\":\"Saturation\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"饱和度\"},{\"identifier\":\"Lightness\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"亮度\"}],\"type\":\"struct\"},\"name\":\"HSL调色\"},{\"identifier\":\"HSVColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Hue\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"色调\"},{\"identifier\":\"Saturation\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"饱和度\"},{\"identifier\":\"Value\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"明度\"}],\"type\":\"struct\"},\"name\":\"HSV调色\"},{\"identifier\":\"PropertyTime\",\"dataType\":{\"specs\":{},\"type\":\"date\"},\"name\":\"时间\"},{\"identifier\":\"Propertypoint\",\"dataType\":{\"specs\":{\"min\":\"-100\",\"max\":\"100\"},\"type\":\"double\"},\"name\":\"浮点型\"},{\"identifier\":\"PropertyCharacter\",\"dataType\":{\"specs\":{\"length\":\"255\"},\"type\":\"text\"},\"name\":\"字符串\"}],\"method\":\"thing.service.property.set\",\"name\":\"set\",\"required\":true,\"callType\":\"sync\",\"desc\":\"属性设置\"},{\"outputData\":[{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\"},{\"identifier\":\"RGBColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Red\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"红色\"},{\"identifier\":\"Green\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"绿色\"},{\"identifier\":\"Blue\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"蓝色\"}],\"type\":\"struct\"},\"name\":\"RGB调色\"},{\"identifier\":\"NightLightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"夜灯开关\"},{\"identifier\":\"WorkMode\",\"dataType\":{\"specs\":{\"0\":\"手动\",\"1\":\"阅读\",\"2\":\"影院\",\"3\":\"夜灯\",\"4\":\"生活\",\"5\":\"柔和\"},\"type\":\"enum\"},\"name\":\"工作模式\"},{\"identifier\":\"ColorTemperature\",\"dataType\":{\"specs\":{\"unit\":\"K\",\"min\":\"2700\",\"unitName\":\"开尔文\",\"max\":\"6500\"},\"type\":\"int\"},\"name\":\"冷暖色温\"},{\"identifier\":\"Brightness\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"明暗度\"},{\"identifier\":\"HSLColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Hue\",\"dataType\":{\"specs\":{\"unit\":\"°\",\"min\":\"0\",\"unitName\":\"度\",\"max\":\"360\"},\"type\":\"int\"},\"name\":\"色调\"},{\"identifier\":\"Saturation\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"饱和度\"},{\"identifier\":\"Lightness\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"亮度\"}],\"type\":\"struct\"},\"name\":\"HSL调色\"},{\"identifier\":\"HSVColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Hue\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"色调\"},{\"identifier\":\"Saturation\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"饱和度\"},{\"identifier\":\"Value\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"明度\"}],\"type\":\"struct\"},\"name\":\"HSV调色\"},{\"identifier\":\"PropertyTime\",\"dataType\":{\"specs\":{},\"type\":\"date\"},\"name\":\"时间\"},{\"identifier\":\"Propertypoint\",\"dataType\":{\"specs\":{\"min\":\"-100\",\"max\":\"100\"},\"type\":\"double\"},\"name\":\"浮点型\"},{\"identifier\":\"PropertyCharacter\",\"dataType\":{\"specs\":{\"length\":\"255\"},\"type\":\"text\"},\"name\":\"字符串\"}],\"identifier\":\"get\",\"inputData\":[\"LightSwitch\",\"RGBColor\",\"NightLightSwitch\",\"WorkMode\",\"ColorTemperature\",\"Brightness\",\"HSLColor\",\"HSVColor\",\"PropertyTime\",\"Propertypoint\",\"PropertyCharacter\"],\"method\":\"thing.service.property.get\",\"name\":\"get\",\"required\":true,\"callType\":\"sync\",\"desc\":\"属性获取\"},{\"outputData\":[{\"identifier\":\"Contrastratio\",\"dataType\":{\"specs\":{\"min\":\"0\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"对比度\"}],\"identifier\":\"Custom\",\"inputData\":[{\"identifier\":\"transparency\",\"dataType\":{\"specs\":{\"min\":\"-100\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"透明度\"}],\"method\":\"thing.service.Custom\",\"name\":\"自定义服务\",\"required\":false,\"callType\":\"async\"}],\"properties\":[{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\",\"accessMode\":\"rw\",\"required\":true},{\"identifier\":\"RGBColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Red\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"红色\"},{\"identifier\":\"Green\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"绿色\"},{\"identifier\":\"Blue\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\" },\"name\":\"蓝色\"}],\"type\":\"struct\"},\"name\":\"RGB调色\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"NightLightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"夜灯开关\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"WorkMode\",\"dataType\":{\"specs\":{\"0\":\"手动\",\"1\":\"阅读\",\"2\":\"影院\",\"3\":\"夜灯\",\"4\":\"生活\",\"5\":\"柔和\"},\"type\":\"enum\"},\"name\":\"工作模式\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"ColorTemperature\",\"dataType\":{\"specs\":{\"unit\":\"K\",\"min\":\"2700\",\"unitName\":\"开尔文\",\"max\":\"6500\"},\"type\":\"int\"},\"name\":\"冷暖色温\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"Brightness\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"明暗度\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"HSLColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Hue\",\"dataType\":{\"specs\":{\"unit\":\"°\",\"min\":\"0\",\"unitName\":\"度\",\"max\":\"360\"},\"type\":\"int\"},\"name\":\"色调\"},{\"identifier\":\"Saturation\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"饱和度\"},{\"identifier\":\"Lightness\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"亮度\"}],\"type\":\"struct\"},\"name\":\"HSL调色\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"HSVColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Hue\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"色调\"},{\"identifier\":\"Saturation\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"饱和度\"},{\"identifier\":\"Value\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"明度\"}],\"type\":\"struct\"},\"name\":\"HSV调色\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"PropertyTime\",\"dataType\":{\"specs\":{},\"type\":\"date\"},\"name\":\"时间\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"Propertypoint\",\"dataType\":{\"specs\":{\"min\":\"-100\",\"max\":\"100\"},\"type\":\"double\"},\"name\":\"浮点型\",\"accessMode\":\"rw\",\"required\":false},{\"identifier\":\"PropertyCharacter\",\"dataType\":{\"specs\":{\"length\":\"255\"},\"type\":\"text\"},\"name\":\"字符串\",\"accessMode\":\"rw\",\"required\":false}],\"events\":[{\"outputData\":[{\"identifier\":\"LightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"主灯开关\"},{\"identifier\":\"RGBColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Red\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"红色\"},{\"identifier\":\"Green\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"绿色\"},{\"identifier\":\"Blue\",\"dataType\":{\"specs\":{\"min\":\"0\",\"unitName\":\"无\",\"max\":\"255\"},\"type\":\"int\"},\"name\":\"蓝色\"}],\"type\":\"struct\"},\"name\":\"RGB调色\"},{\"identifier\":\"NightLightSwitch\",\"dataType\":{\"specs\":{\"0\":\"关闭\",\"1\":\"开启\"},\"type\":\"bool\"},\"name\":\"夜灯开关\"},{\"identifier\":\"WorkMode\",\"dataType\":{\"specs\":{\"0\":\"手动\",\"1\":\"阅读\",\"2\":\"影院\",\"3\":\"夜灯\",\"4\":\"生活\",\"5\":\"柔和\"},\"type\":\"enum\"},\"name\":\"工作模式\"},{\"identifier\":\"ColorTemperature\",\"dataType\":{\"specs\":{\"unit\":\"K\",\"min\":\"2700\",\"unitName\":\"开尔文\",\"max\":\"6500\"},\"type\":\"int\"},\"name\":\"冷暖色温\"},{\"identifier\":\"Brightness\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"明暗度\"},{\"identifier\":\"HSLColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Hue\",\"dataType\":{\"specs\":{\"unit\":\"°\",\"min\":\"0\",\"unitName\":\"度\",\"max\":\"360\"},\"type\":\"int\"},\"name\":\"色调\"},{\"identifier\":\"Saturation\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"饱和度\"},{\"identifier\":\"Lightness\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"亮度\"}],\"type\":\"struct\"},\"name\":\"HSL调色\"},{\"identifier\":\"HSVColor\",\"dataType\":{\"specs\":[{\"identifier\":\"Hue\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"色调\"},{\"identifier\":\"Saturation\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"饱和度\"},{\"identifier\":\"Value\",\"dataType\":{\"specs\":{\"unit\":\"%\",\"min\":\"0\",\"unitName\":\"百分比\",\"max\":\"100\"},\"type\":\"int\"},\"name\":\"明度\"}],\"type\":\"struct\"},\"name\":\"HSV调色\"},{\"identifier\":\"PropertyTime\",\"dataType\":{\"specs\":{},\"type\":\"date\"},\"name\":\"时间\"},{\"identifier\":\"Propertypoint\",\"dataType\":{\"specs\":{\"min\":\"-100\",\"max\":\"100\"},\"type\":\"double\"},\"name\":\"浮点型\"},{\"identifier\":\"PropertyCharacter\",\"dataType\":{\"specs\":{\"length\":\"255\"},\"type\":\"text\"},\"name\":\"字符串\"}],\"identifier\":\"post\",\"method\":\"thing.event.property.post\",\"name\":\"post\",\"type\":\"info\",\"required\":true,\"desc\":\"属性上报\"},{\"outputData\":[{\"identifier\":\"ErrorCode\",\"dataType\":{\"specs\":{\"0\":\"恢复正常\"},\"type\":\"enum\"},\"name\":\"故障代码\"}],\"identifier\":\"Error\",\"method\":\"thing.event.Error.post\",\"name\":\"故障上报\",\"type\":\"info\",\"required\":true}]}";
  46. #else
  47. const char TSL_STRING[] = "add tsl in there";
  48. #endif
  49. /* user sample context struct. */
  50. typedef struct _sample_context {
  51. void* thing;
  52. int cloud_connected;
  53. int thing_enabled;
  54. int service_custom_input_transparency;
  55. int service_custom_output_contrastratio;
  56. #ifdef SERVICE_OTA_ENABLED
  57. char ota_buffer[OTA_BUFFER_SIZE];
  58. #endif /* SERVICE_OTA_ENABLED */
  59. } sample_context_t;
  60. sample_context_t g_sample_context;
  61. #ifdef SERVICE_OTA_ENABLED
  62. #ifndef SERVICE_COTA_ENABLED
  63. /* callback function for fota service. */
  64. static void fota_callback(service_fota_callback_type_t callback_type, const char* version)
  65. {
  66. sample_context_t* sample;
  67. assert(callback_type < service_fota_callback_type_number);
  68. sample = &g_sample_context;
  69. /* temporarily disable thing when ota service invoked */
  70. sample->thing_enabled = 0;
  71. linkkit_invoke_fota_service(sample->ota_buffer, OTA_BUFFER_SIZE);
  72. sample->thing_enabled = 1;
  73. /* reboot the device... */
  74. }
  75. #else /* SERVICE_COTA_ENABLED */
  76. static void cota_callback(service_cota_callback_type_t callback_type,
  77. const char* configid,
  78. uint32_t configsize,
  79. const char* gettype,
  80. const char* sign,
  81. const char* signmethod,
  82. const char* cota_url)
  83. {
  84. sample_context_t* sample;
  85. assert(callback_type < service_cota_callback_type_number);
  86. sample = &g_sample_context;
  87. /* temporarily disable thing when ota service invoked */
  88. sample->thing_enabled = 0;
  89. linkkit_invoke_cota_service(sample->ota_buffer, OTA_BUFFER_SIZE);
  90. sample->thing_enabled = 1;
  91. /* update config... */
  92. }
  93. #endif /* SERVICE_COTA_ENABLED */
  94. #endif /* SERVICE_OTA_ENABLED */
  95. static int on_connect(void* ctx)
  96. {
  97. sample_context_t* sample = ctx;
  98. sample->cloud_connected = 1;
  99. LINKKIT_PRINTF("cloud is connected\n");
  100. return 0;
  101. }
  102. static int on_disconnect(void* ctx)
  103. {
  104. sample_context_t* sample = ctx;
  105. sample->cloud_connected = 0;
  106. LINKKIT_PRINTF("cloud is disconnect\n");
  107. return 0;
  108. }
  109. static int raw_data_arrived(void* thing_id, void* data, int len, void* ctx)
  110. {
  111. char raw_data[128] = {0};
  112. LINKKIT_PRINTF("raw data arrived,len:%d\n", len);
  113. /* do user's raw data process logical here. */
  114. /* ............................... */
  115. /* user's raw data process logical complete */
  116. snprintf(raw_data, sizeof(raw_data), "test down raw reply data %lld", (long long)HAL_UptimeMs());
  117. linkkit_invoke_raw_service(thing_id, 0, raw_data, strlen(raw_data));
  118. return 0;
  119. }
  120. static int thing_create(void* thing_id, void* ctx)
  121. {
  122. sample_context_t* sample = ctx;
  123. LINKKIT_PRINTF("new thing@%p created.\n", thing_id);
  124. sample->thing = thing_id;
  125. return 0;
  126. }
  127. static int thing_enable(void* thing_id, void* ctx)
  128. {
  129. sample_context_t* sample = ctx;
  130. sample->thing_enabled = 1;
  131. return 0;
  132. }
  133. static int thing_disable(void* thing, void* ctx)
  134. {
  135. sample_context_t* sample = ctx;
  136. sample->thing_enabled = 0;
  137. return 0;
  138. }
  139. #ifdef RRPC_ENABLED
  140. static int handle_service_custom(sample_context_t* sample, void* thing, char* service_identifier, int request_id, int rrpc)
  141. #else
  142. static int handle_service_custom(sample_context_t* sample, void* thing, char* service_identifier, int request_id)
  143. #endif /* RRPC_ENABLED */
  144. {
  145. char identifier[128] = {0};
  146. /*
  147. * get iutput value.
  148. */
  149. snprintf(identifier, sizeof(identifier), "%s.%s", service_identifier, "transparency");
  150. linkkit_get_value(linkkit_method_get_service_input_value, thing, identifier, &sample->service_custom_input_transparency, NULL);
  151. /*
  152. * set output value according to user's process result.
  153. */
  154. snprintf(identifier, sizeof(identifier), "%s.%s", service_identifier, "Contrastratio");
  155. sample->service_custom_output_contrastratio = sample->service_custom_input_transparency >= 0 ? sample->service_custom_input_transparency : sample->service_custom_input_transparency * -1;
  156. linkkit_set_value(linkkit_method_set_service_output_value, thing, identifier, &sample->service_custom_output_contrastratio, NULL);
  157. #ifdef RRPC_ENABLED
  158. linkkit_answer_service(thing, service_identifier, request_id, 200, rrpc);
  159. #else
  160. linkkit_answer_service(thing, service_identifier, request_id, 200);
  161. #endif /* RRPC_ENABLED */
  162. return 0;
  163. }
  164. #ifdef RRPC_ENABLED
  165. static int thing_call_service(void* thing_id, char* service, int request_id, int rrpc, void* ctx)
  166. #else
  167. static int thing_call_service(void* thing_id, char* service, int request_id, void* ctx)
  168. #endif /* RRPC_ENABLED */
  169. {
  170. sample_context_t* sample = ctx;
  171. LINKKIT_PRINTF("service(%s) requested, id: thing@%p, request id:%d\n",
  172. service ? service : "NULL", thing_id, request_id);
  173. if (strcmp(service, "Custom") == 0) {
  174. #ifdef RRPC_ENABLED
  175. handle_service_custom(sample, thing_id, service, request_id, rrpc);
  176. #else
  177. handle_service_custom(sample, thing_id, service, request_id);
  178. #endif /* RRPC_ENABLED */
  179. }
  180. return 0;
  181. }
  182. static int thing_prop_changed(void* thing_id, char* property, void* ctx)
  183. {
  184. char* value_str = NULL;
  185. char property_buf[64] = {0};
  186. /* get new property value */
  187. if (strstr(property, "HSVColor") != 0) {
  188. int hue, saturation, value;
  189. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Hue");
  190. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &hue, &value_str);
  191. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Saturation");
  192. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &saturation, &value_str);
  193. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Value");
  194. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &value, &value_str);
  195. LINKKIT_PRINTF("property(%s), Hue:%d, Saturation:%d, Value:%d\n", property, hue, saturation, value);
  196. /* XXX: do user's process logical here. */
  197. } else if (strstr(property, "HSLColor") != 0) {
  198. int hue, saturation, lightness;
  199. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Hue");
  200. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &hue, &value_str);
  201. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Saturation");
  202. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &saturation, &value_str);
  203. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Lightness");
  204. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &lightness, &value_str);
  205. LINKKIT_PRINTF("property(%s), Hue:%d, Saturation:%d, Lightness:%d\n", property, hue, saturation, lightness);
  206. /* XXX: do user's process logical here. */
  207. } else if (strstr(property, "RGBColor") != 0) {
  208. int red, green, blue;
  209. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Red");
  210. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &red, &value_str);
  211. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Green");
  212. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &green, &value_str);
  213. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Blue");
  214. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &blue, &value_str);
  215. LINKKIT_PRINTF("property(%s), Red:%d, Green:%d, Blue:%d\n", property, red, green, blue);
  216. /* XXX: do user's process logical here. */
  217. } else if (strstr(property, "structProperty") != 0) {
  218. int childINTName = 0;
  219. int childFLOATName = 0;
  220. int childDATEName = 0;
  221. int childTEXTName = 0;
  222. int childBOOLName = 0;
  223. int childENUMName = 0;
  224. int childDOUBLEName = 0;
  225. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "structchildINTd1e197e");
  226. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &childINTName, &value_str);
  227. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "structchildFLOATcdef1f2");
  228. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &childFLOATName, &value_str);
  229. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "structchildDATE228857b");
  230. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &childDATEName, &value_str);
  231. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "structchildTEXT5ad129f");
  232. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &childTEXTName, &value_str);
  233. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "structchildBOOLea5c7ba");
  234. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &childBOOLName, &value_str);
  235. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "structchildENUMf600fe3");
  236. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &childENUMName, &value_str);
  237. snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "structchildDOUBLEff6bba6");
  238. linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &childDOUBLEName, &value_str);
  239. LINKKIT_PRINTF("property(%s), childINTName:%d, childFLOATName:%d, childDATEName:%d, childTEXTName:%d, childBOOLName:%d, childENUMName:%d, childDOUBLEName:%d\n",\
  240. property, childINTName, childFLOATName, childDATEName, childTEXTName, childBOOLName, childENUMName, childDOUBLEName);
  241. } else {
  242. linkkit_get_value(linkkit_method_get_property_value, thing_id, property, NULL, &value_str);
  243. LINKKIT_PRINTF("#### property(%s) new value set: %s ####\n", property ? property : "NULL", value_str ? value_str : "NULL");
  244. }
  245. /* do user's process logical here. */
  246. linkkit_trigger_event(thing_id, EVENT_PROPERTY_POST_IDENTIFIER, property);
  247. return 0;
  248. }
  249. static linkkit_ops_t alinkops = {
  250. .on_connect = on_connect,
  251. .on_disconnect = on_disconnect,
  252. .raw_data_arrived = raw_data_arrived,
  253. .thing_create = thing_create,
  254. .thing_enable = thing_enable,
  255. .thing_disable = thing_disable,
  256. .thing_call_service = thing_call_service,
  257. .thing_prop_changed = thing_prop_changed,
  258. };
  259. static unsigned long long uptime_sec(void)
  260. {
  261. static unsigned long long start_time = 0;
  262. if (start_time == 0) {
  263. start_time = HAL_UptimeMs();
  264. }
  265. return (HAL_UptimeMs() - start_time) / 1000;
  266. }
  267. #if 1
  268. static int post_all_prop(sample_context_t* sample)
  269. {
  270. return linkkit_trigger_event(sample->thing, EVENT_PROPERTY_POST_IDENTIFIER, NULL);
  271. }
  272. #endif
  273. static int post_event_error(sample_context_t* sample)
  274. {
  275. char event_output_identifier[64];
  276. snprintf(event_output_identifier, sizeof(event_output_identifier), "%s.%s", EVENT_ERROR_IDENTIFIER, EVENT_ERROR_OUTPUT_INFO_IDENTIFIER);
  277. int errorCode = 0;
  278. linkkit_set_value(linkkit_method_set_event_output_value,
  279. sample->thing,
  280. event_output_identifier,
  281. &errorCode, NULL);
  282. return linkkit_trigger_event(sample->thing, EVENT_ERROR_IDENTIFIER, NULL);
  283. }
  284. #if 0
  285. static int post_event_fault_alert(sample_context_t* sample)
  286. {
  287. char event_output_identifier[64];
  288. snprintf(event_output_identifier, sizeof(event_output_identifier), "%s.%s", EVENT_ERROR_IDENTIFIER, EVENT_ERROR_OUTPUT_INFO_IDENTIFIER);
  289. int errorCode = 0;
  290. linkkit_set_value(linkkit_method_set_event_output_value,
  291. sample->thing,
  292. event_output_identifier,
  293. &errorCode, NULL);
  294. return linkkit_trigger_event(sample->thing, EVENT_ERROR_IDENTIFIER, NULL);
  295. }
  296. static int upload_raw_data(sample_context_t* sample)
  297. {
  298. char raw_data[128] = {0};
  299. snprintf(raw_data, sizeof(raw_data), "test up raw data %lld", HAL_UptimeMs());
  300. return linkkit_invoke_raw_service(sample->thing, 1, raw_data, strlen(raw_data));
  301. }
  302. #endif
  303. static int is_active(sample_context_t* sample)
  304. {
  305. return sample->cloud_connected && sample->thing_enabled;
  306. }
  307. #ifdef MQTT_ID2_AUTH
  308. #include "tfs.h"
  309. #endif
  310. int main(int argc, char* argv[])
  311. {
  312. sample_context_t* sample_ctx = &g_sample_context;
  313. int execution_time = 0;
  314. int get_tsl_from_cloud = 0;
  315. int exit = 0;
  316. unsigned long long now = 0;
  317. unsigned long long prev_sec = 0;
  318. HAL_SetProductKey(DM_PRODUCT_KEY_1);
  319. #ifdef MQTT_ID2_AUTH
  320. char __device_id2[TFS_ID2_LEN + 1];
  321. HAL_GetID2(__device_id2);
  322. HAL_SetDeviceName(__device_id2);
  323. #else
  324. HAL_SetDeviceName(DM_DEVICE_NAME_1);
  325. #endif
  326. HAL_SetDeviceSecret(DM_DEVICE_SECRET_1);
  327. #if !(WIN32)
  328. int opt;
  329. while ((opt = getopt(argc, argv, "t:g:h")) != -1) {
  330. switch (opt) {
  331. case 't':
  332. execution_time = atoi(optarg);
  333. break;
  334. case 'g':
  335. get_tsl_from_cloud = atoi(optarg);
  336. break;
  337. case 'h':
  338. LINKKIT_PRINTF("-t to specify sample execution time period(minutes); -g to specify if get tsl from cloud(0: not, !0: yes).\n");
  339. return 0;
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. #endif
  346. execution_time = execution_time < 1 ? 1 : execution_time;
  347. LINKKIT_PRINTF("sample execution time: %d minutes\n", execution_time);
  348. LINKKIT_PRINTF("%s tsl from cloud\n", get_tsl_from_cloud == 0 ? "Not get" : "get");
  349. memset(sample_ctx, 0, sizeof(sample_context_t));
  350. sample_ctx->thing_enabled = 1;
  351. linkkit_start(8, get_tsl_from_cloud, linkkit_loglevel_debug, &alinkops, linkkit_cloud_domain_sh, sample_ctx);
  352. if (!get_tsl_from_cloud) {
  353. linkkit_set_tsl(TSL_STRING, strlen(TSL_STRING));
  354. }
  355. #ifdef SERVICE_OTA_ENABLED
  356. #ifndef SERVICE_COTA_ENABLED
  357. LINKKIT_PRINTF("FOTA start\n");
  358. linkkit_fota_init(fota_callback);
  359. #else /* SERVICE_COTA_ENABLED */
  360. LINKKIT_PRINTF("COTA_start\n");
  361. linkkit_cota_init(cota_callback);
  362. linkkit_invoke_cota_get_config("product","file","",NULL);
  363. #endif /* SERVICE_COTA_ENABLED */
  364. #endif /* SERVICE_OTA_ENABLED */
  365. while (1) {
  366. linkkit_dispatch();
  367. now = uptime_sec();
  368. if (prev_sec == now) {
  369. #ifdef CMP_SUPPORT_MULTI_THREAD
  370. HAL_SleepMs(100);
  371. #else
  372. linkkit_yield(100);
  373. #endif
  374. continue;
  375. }
  376. /* about 30 seconds, assume trigger post property event about every 30s. */
  377. if (now % 30 == 0 && is_active(sample_ctx)) {
  378. post_all_prop(sample_ctx);
  379. }
  380. #if 0
  381. /* about 31 seconds, assume invoke raw up service about every 31s. */
  382. if (now % 31 == 0 && is_active(sample_ctx)) {
  383. upload_raw_data(sample_ctx);
  384. }
  385. /* about 60 seconds, assume trigger event about every 60s. */
  386. if (now % 60 == 0 && is_active(sample_ctx)) {
  387. post_event_fault_alert(sample_ctx);
  388. }
  389. #endif
  390. /* about 60 seconds, assume trigger event about every 60s. */
  391. if (now % 60 == 0 && is_active(sample_ctx)) {
  392. post_event_error(sample_ctx);
  393. }
  394. if (exit) break;
  395. /* after all, this is an sample, give a chance to return... */
  396. /* modify this value for this sample executaion time period */
  397. if (now > 60 * execution_time) exit = 1;
  398. prev_sec = now;
  399. }
  400. linkkit_end();
  401. return 0;
  402. }