EDP KIT from OneNet
Dependents: 8-0_OneNet_IoT_demo STM32F103C8T6_OneNet_IoT
EdpKit.cpp@0:79de700849c5, 2017-03-29 (annotated)
- Committer:
- TaylorGy
- Date:
- Wed Mar 29 03:07:27 2017 +0000
- Revision:
- 0:79de700849c5
EDP KIT from OneNet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TaylorGy | 0:79de700849c5 | 1 | #include <stdlib.h> |
TaylorGy | 0:79de700849c5 | 2 | #include <stdio.h> |
TaylorGy | 0:79de700849c5 | 3 | #include <string.h> |
TaylorGy | 0:79de700849c5 | 4 | #include <time.h> |
TaylorGy | 0:79de700849c5 | 5 | #include <assert.h> |
TaylorGy | 0:79de700849c5 | 6 | #include "EdpKit.h" |
TaylorGy | 0:79de700849c5 | 7 | |
TaylorGy | 0:79de700849c5 | 8 | #define MKFUN_PACKET_SAVE_DATA(TYPE, NAME) \ |
TaylorGy | 0:79de700849c5 | 9 | EdpPacket* PacketSavedata##NAME(SaveDataType type, \ |
TaylorGy | 0:79de700849c5 | 10 | const char* dst_devid, \ |
TaylorGy | 0:79de700849c5 | 11 | const char* ds_id, \ |
TaylorGy | 0:79de700849c5 | 12 | TYPE value, \ |
TaylorGy | 0:79de700849c5 | 13 | time_t at, \ |
TaylorGy | 0:79de700849c5 | 14 | const char* token){ \ |
TaylorGy | 0:79de700849c5 | 15 | EdpPacket* pkg = NULL; \ |
TaylorGy | 0:79de700849c5 | 16 | switch (type){ \ |
TaylorGy | 0:79de700849c5 | 17 | case kTypeFullJson: \ |
TaylorGy | 0:79de700849c5 | 18 | pkg = PacketSavedataType1##NAME(dst_devid, ds_id, \ |
TaylorGy | 0:79de700849c5 | 19 | value, at, token); \ |
TaylorGy | 0:79de700849c5 | 20 | break; \ |
TaylorGy | 0:79de700849c5 | 21 | case kTypeSimpleJsonWithoutTime: \ |
TaylorGy | 0:79de700849c5 | 22 | pkg = PacketSavedataType2##NAME(dst_devid, ds_id, \ |
TaylorGy | 0:79de700849c5 | 23 | value, at, token); \ |
TaylorGy | 0:79de700849c5 | 24 | break; \ |
TaylorGy | 0:79de700849c5 | 25 | case kTypeSimpleJsonWithTime: \ |
TaylorGy | 0:79de700849c5 | 26 | pkg = PacketSavedataType3##NAME(dst_devid, ds_id, \ |
TaylorGy | 0:79de700849c5 | 27 | value, at, token); \ |
TaylorGy | 0:79de700849c5 | 28 | break; \ |
TaylorGy | 0:79de700849c5 | 29 | default: \ |
TaylorGy | 0:79de700849c5 | 30 | break; \ |
TaylorGy | 0:79de700849c5 | 31 | } \ |
TaylorGy | 0:79de700849c5 | 32 | \ |
TaylorGy | 0:79de700849c5 | 33 | return pkg; \ |
TaylorGy | 0:79de700849c5 | 34 | } |
TaylorGy | 0:79de700849c5 | 35 | |
TaylorGy | 0:79de700849c5 | 36 | #define MKFUN_PACKET_SAVE_DATA_TYPE1(TYPE, NAME, MED) \ |
TaylorGy | 0:79de700849c5 | 37 | static EdpPacket* PacketSavedataType1##NAME(const char* dst_devid, \ |
TaylorGy | 0:79de700849c5 | 38 | const char* ds_id, \ |
TaylorGy | 0:79de700849c5 | 39 | TYPE value, \ |
TaylorGy | 0:79de700849c5 | 40 | time_t at, \ |
TaylorGy | 0:79de700849c5 | 41 | const char* token){ \ |
TaylorGy | 0:79de700849c5 | 42 | cJSON* json_obj = NULL; \ |
TaylorGy | 0:79de700849c5 | 43 | cJSON* ds_array = NULL; \ |
TaylorGy | 0:79de700849c5 | 44 | cJSON* json_obj_ds = NULL; \ |
TaylorGy | 0:79de700849c5 | 45 | cJSON* dp_array = NULL; \ |
TaylorGy | 0:79de700849c5 | 46 | cJSON* dp_item = NULL; \ |
TaylorGy | 0:79de700849c5 | 47 | char ss[100] = {0}; \ |
TaylorGy | 0:79de700849c5 | 48 | EdpPacket* pkg = NULL; \ |
TaylorGy | 0:79de700849c5 | 49 | \ |
TaylorGy | 0:79de700849c5 | 50 | json_obj = cJSON_CreateObject(); \ |
TaylorGy | 0:79de700849c5 | 51 | \ |
TaylorGy | 0:79de700849c5 | 52 | if(token) { \ |
TaylorGy | 0:79de700849c5 | 53 | cJSON_AddStringToObject(json_obj, "token", token); \ |
TaylorGy | 0:79de700849c5 | 54 | } \ |
TaylorGy | 0:79de700849c5 | 55 | \ |
TaylorGy | 0:79de700849c5 | 56 | ds_array = cJSON_CreateArray(); \ |
TaylorGy | 0:79de700849c5 | 57 | json_obj_ds = cJSON_CreateObject(); \ |
TaylorGy | 0:79de700849c5 | 58 | \ |
TaylorGy | 0:79de700849c5 | 59 | cJSON_AddStringToObject(json_obj_ds, "id", ds_id); \ |
TaylorGy | 0:79de700849c5 | 60 | \ |
TaylorGy | 0:79de700849c5 | 61 | dp_array = cJSON_CreateArray(); \ |
TaylorGy | 0:79de700849c5 | 62 | dp_item = cJSON_CreateObject(); \ |
TaylorGy | 0:79de700849c5 | 63 | \ |
TaylorGy | 0:79de700849c5 | 64 | FormatAt(ss, 100, at); \ |
TaylorGy | 0:79de700849c5 | 65 | cJSON_AddStringToObject(dp_item, "at", ss); \ |
TaylorGy | 0:79de700849c5 | 66 | cJSON_Add##MED##ToObject(dp_item, "value", value); \ |
TaylorGy | 0:79de700849c5 | 67 | cJSON_AddItemToArray(dp_array, dp_item); \ |
TaylorGy | 0:79de700849c5 | 68 | \ |
TaylorGy | 0:79de700849c5 | 69 | cJSON_AddItemToObject(json_obj_ds, "datapoints", dp_array); \ |
TaylorGy | 0:79de700849c5 | 70 | cJSON_AddItemToArray(ds_array, json_obj_ds); \ |
TaylorGy | 0:79de700849c5 | 71 | cJSON_AddItemToObject(json_obj, "datastreams", ds_array); \ |
TaylorGy | 0:79de700849c5 | 72 | \ |
TaylorGy | 0:79de700849c5 | 73 | pkg = PacketSavedataJson(dst_devid, json_obj, \ |
TaylorGy | 0:79de700849c5 | 74 | kTypeFullJson); \ |
TaylorGy | 0:79de700849c5 | 75 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 76 | \ |
TaylorGy | 0:79de700849c5 | 77 | return pkg; \ |
TaylorGy | 0:79de700849c5 | 78 | } |
TaylorGy | 0:79de700849c5 | 79 | |
TaylorGy | 0:79de700849c5 | 80 | #define MKFUN_PACKET_SAVE_DATA_TYPE2(TYPE, NAME, MED) \ |
TaylorGy | 0:79de700849c5 | 81 | static EdpPacket* PacketSavedataType2##NAME(const char* dst_devid, \ |
TaylorGy | 0:79de700849c5 | 82 | const char* ds_id, \ |
TaylorGy | 0:79de700849c5 | 83 | TYPE value, \ |
TaylorGy | 0:79de700849c5 | 84 | time_t at, \ |
TaylorGy | 0:79de700849c5 | 85 | const char* token) \ |
TaylorGy | 0:79de700849c5 | 86 | { \ |
TaylorGy | 0:79de700849c5 | 87 | cJSON* json_obj = NULL; \ |
TaylorGy | 0:79de700849c5 | 88 | EdpPacket* pkg = NULL; \ |
TaylorGy | 0:79de700849c5 | 89 | \ |
TaylorGy | 0:79de700849c5 | 90 | json_obj = cJSON_CreateObject(); \ |
TaylorGy | 0:79de700849c5 | 91 | cJSON_Add##MED##ToObject(json_obj, ds_id, value); \ |
TaylorGy | 0:79de700849c5 | 92 | \ |
TaylorGy | 0:79de700849c5 | 93 | pkg = PacketSavedataJson(dst_devid, json_obj, \ |
TaylorGy | 0:79de700849c5 | 94 | kTypeSimpleJsonWithoutTime); \ |
TaylorGy | 0:79de700849c5 | 95 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 96 | \ |
TaylorGy | 0:79de700849c5 | 97 | return pkg; \ |
TaylorGy | 0:79de700849c5 | 98 | } |
TaylorGy | 0:79de700849c5 | 99 | |
TaylorGy | 0:79de700849c5 | 100 | |
TaylorGy | 0:79de700849c5 | 101 | #define MKFUN_PACKET_SAVE_DATA_TYPE3(TYPE, NAME, MED) \ |
TaylorGy | 0:79de700849c5 | 102 | static EdpPacket* PacketSavedataType3##NAME(const char* dst_devid, \ |
TaylorGy | 0:79de700849c5 | 103 | const char* ds_id, \ |
TaylorGy | 0:79de700849c5 | 104 | TYPE value, \ |
TaylorGy | 0:79de700849c5 | 105 | time_t at, \ |
TaylorGy | 0:79de700849c5 | 106 | const char* token){ \ |
TaylorGy | 0:79de700849c5 | 107 | cJSON* json_obj = NULL; \ |
TaylorGy | 0:79de700849c5 | 108 | cJSON* json_obj_ds = NULL; \ |
TaylorGy | 0:79de700849c5 | 109 | char ss[100] = {0}; \ |
TaylorGy | 0:79de700849c5 | 110 | EdpPacket* pkg = NULL; \ |
TaylorGy | 0:79de700849c5 | 111 | \ |
TaylorGy | 0:79de700849c5 | 112 | json_obj = cJSON_CreateObject(); \ |
TaylorGy | 0:79de700849c5 | 113 | FormatAt(ss, 100, at); \ |
TaylorGy | 0:79de700849c5 | 114 | json_obj_ds = cJSON_CreateObject(); \ |
TaylorGy | 0:79de700849c5 | 115 | cJSON_Add##MED##ToObject(json_obj_ds, ss, value); \ |
TaylorGy | 0:79de700849c5 | 116 | cJSON_AddItemToObject(json_obj, ds_id, json_obj_ds); \ |
TaylorGy | 0:79de700849c5 | 117 | \ |
TaylorGy | 0:79de700849c5 | 118 | pkg = PacketSavedataJson(dst_devid, json_obj, \ |
TaylorGy | 0:79de700849c5 | 119 | kTypeSimpleJsonWithTime); \ |
TaylorGy | 0:79de700849c5 | 120 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 121 | \ |
TaylorGy | 0:79de700849c5 | 122 | return pkg; \ |
TaylorGy | 0:79de700849c5 | 123 | } |
TaylorGy | 0:79de700849c5 | 124 | |
TaylorGy | 0:79de700849c5 | 125 | #define MKFUN_UNPACK_SAVE_DATA(TYPE, NAME) \ |
TaylorGy | 0:79de700849c5 | 126 | int32 UnpackSavedata##NAME(SaveDataType type, \ |
TaylorGy | 0:79de700849c5 | 127 | EdpPacket* pkg, \ |
TaylorGy | 0:79de700849c5 | 128 | char** ds_id, \ |
TaylorGy | 0:79de700849c5 | 129 | TYPE* value) \ |
TaylorGy | 0:79de700849c5 | 130 | { \ |
TaylorGy | 0:79de700849c5 | 131 | int ret = -1; \ |
TaylorGy | 0:79de700849c5 | 132 | switch (type){ \ |
TaylorGy | 0:79de700849c5 | 133 | case kTypeFullJson: \ |
TaylorGy | 0:79de700849c5 | 134 | ret = UnpackSavedataType1##NAME(pkg, ds_id, value); \ |
TaylorGy | 0:79de700849c5 | 135 | break; \ |
TaylorGy | 0:79de700849c5 | 136 | case kTypeSimpleJsonWithoutTime: \ |
TaylorGy | 0:79de700849c5 | 137 | ret = UnpackSavedataType2##NAME(pkg, ds_id, value); \ |
TaylorGy | 0:79de700849c5 | 138 | break; \ |
TaylorGy | 0:79de700849c5 | 139 | case kTypeSimpleJsonWithTime: \ |
TaylorGy | 0:79de700849c5 | 140 | ret = UnpackSavedataType3##NAME(pkg, ds_id, value); \ |
TaylorGy | 0:79de700849c5 | 141 | break; \ |
TaylorGy | 0:79de700849c5 | 142 | default: \ |
TaylorGy | 0:79de700849c5 | 143 | break; \ |
TaylorGy | 0:79de700849c5 | 144 | } \ |
TaylorGy | 0:79de700849c5 | 145 | \ |
TaylorGy | 0:79de700849c5 | 146 | return ret; \ |
TaylorGy | 0:79de700849c5 | 147 | } |
TaylorGy | 0:79de700849c5 | 148 | |
TaylorGy | 0:79de700849c5 | 149 | #define MKFUN_UNPACK_SAVE_DATA_TYPE1(TYPE, NAME, MED) \ |
TaylorGy | 0:79de700849c5 | 150 | static int32 UnpackSavedataType1##NAME(EdpPacket* pkg, \ |
TaylorGy | 0:79de700849c5 | 151 | char** ds_id, \ |
TaylorGy | 0:79de700849c5 | 152 | TYPE* value) \ |
TaylorGy | 0:79de700849c5 | 153 | { \ |
TaylorGy | 0:79de700849c5 | 154 | cJSON* json_obj = NULL; \ |
TaylorGy | 0:79de700849c5 | 155 | cJSON* ds_array = NULL; \ |
TaylorGy | 0:79de700849c5 | 156 | cJSON* ds_item = NULL; \ |
TaylorGy | 0:79de700849c5 | 157 | char* id = NULL; \ |
TaylorGy | 0:79de700849c5 | 158 | cJSON* dp_array = NULL; \ |
TaylorGy | 0:79de700849c5 | 159 | cJSON* dp_item = NULL; \ |
TaylorGy | 0:79de700849c5 | 160 | char* valuestring = NULL; \ |
TaylorGy | 0:79de700849c5 | 161 | \ |
TaylorGy | 0:79de700849c5 | 162 | if(0 != UnpackSavedataJson(pkg, &json_obj)) \ |
TaylorGy | 0:79de700849c5 | 163 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 164 | \ |
TaylorGy | 0:79de700849c5 | 165 | ds_array = cJSON_GetObjectItem(json_obj, "datastreams"); \ |
TaylorGy | 0:79de700849c5 | 166 | if((0 == ds_array) || (1 != cJSON_GetArraySize(ds_array))) \ |
TaylorGy | 0:79de700849c5 | 167 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 168 | \ |
TaylorGy | 0:79de700849c5 | 169 | ds_item = cJSON_GetArrayItem(ds_array, 0); \ |
TaylorGy | 0:79de700849c5 | 170 | if(0 == ds_item) \ |
TaylorGy | 0:79de700849c5 | 171 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 172 | \ |
TaylorGy | 0:79de700849c5 | 173 | id = cJSON_GetObjectItem(ds_item, "id")->valuestring; \ |
TaylorGy | 0:79de700849c5 | 174 | *ds_id = (char*)malloc(strlen(id)+1); \ |
TaylorGy | 0:79de700849c5 | 175 | memcpy((char*)(*ds_id), id, strlen(id)+1); \ |
TaylorGy | 0:79de700849c5 | 176 | \ |
TaylorGy | 0:79de700849c5 | 177 | dp_array = cJSON_GetObjectItem(ds_item, "datapoints"); \ |
TaylorGy | 0:79de700849c5 | 178 | if((0 == dp_array) || (1 != cJSON_GetArraySize(dp_array))) \ |
TaylorGy | 0:79de700849c5 | 179 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 180 | \ |
TaylorGy | 0:79de700849c5 | 181 | dp_item = cJSON_GetArrayItem(dp_array, 0); \ |
TaylorGy | 0:79de700849c5 | 182 | |
TaylorGy | 0:79de700849c5 | 183 | |
TaylorGy | 0:79de700849c5 | 184 | #define UNPACK_SAVE_DATA_TYPE1_END_STRING \ |
TaylorGy | 0:79de700849c5 | 185 | valuestring = cJSON_GetObjectItem(dp_item, "value")->valuestring; \ |
TaylorGy | 0:79de700849c5 | 186 | *value = (char*)malloc(strlen(valuestring)+1); \ |
TaylorGy | 0:79de700849c5 | 187 | memcpy((char*)(*value), valuestring, strlen(valuestring)+1); \ |
TaylorGy | 0:79de700849c5 | 188 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 189 | return 0; \ |
TaylorGy | 0:79de700849c5 | 190 | } |
TaylorGy | 0:79de700849c5 | 191 | |
TaylorGy | 0:79de700849c5 | 192 | #define UNPACK_SAVE_DATA_TYPE1_END_NUMBER(TYPE) \ |
TaylorGy | 0:79de700849c5 | 193 | *value = cJSON_GetObjectItem(dp_item, "value")->value##TYPE; \ |
TaylorGy | 0:79de700849c5 | 194 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 195 | return 0; \ |
TaylorGy | 0:79de700849c5 | 196 | } |
TaylorGy | 0:79de700849c5 | 197 | |
TaylorGy | 0:79de700849c5 | 198 | #define MKFUN_UNPACK_SAVE_DATA_TYPE2(TYPE, NAME, MED) \ |
TaylorGy | 0:79de700849c5 | 199 | static int32 UnpackSavedataType2##NAME(EdpPacket* pkg, \ |
TaylorGy | 0:79de700849c5 | 200 | char** ds_id, \ |
TaylorGy | 0:79de700849c5 | 201 | TYPE* value) \ |
TaylorGy | 0:79de700849c5 | 202 | { \ |
TaylorGy | 0:79de700849c5 | 203 | cJSON* json_obj = NULL; \ |
TaylorGy | 0:79de700849c5 | 204 | cJSON* json_child = NULL; \ |
TaylorGy | 0:79de700849c5 | 205 | size_t len = 0; \ |
TaylorGy | 0:79de700849c5 | 206 | char* valuestring = NULL; \ |
TaylorGy | 0:79de700849c5 | 207 | \ |
TaylorGy | 0:79de700849c5 | 208 | if(0 != UnpackSavedataJson(pkg, &json_obj)) \ |
TaylorGy | 0:79de700849c5 | 209 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 210 | if (!json_obj->child){ \ |
TaylorGy | 0:79de700849c5 | 211 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 212 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 213 | } \ |
TaylorGy | 0:79de700849c5 | 214 | json_child = json_obj->child; \ |
TaylorGy | 0:79de700849c5 | 215 | len = strlen(json_child->string) + 1; \ |
TaylorGy | 0:79de700849c5 | 216 | *ds_id = (char*)malloc(len); \ |
TaylorGy | 0:79de700849c5 | 217 | memcpy((char*)(*ds_id), json_child->string, len); \ |
TaylorGy | 0:79de700849c5 | 218 | |
TaylorGy | 0:79de700849c5 | 219 | #define UNPACK_SAVE_DATA_TYPE23_END_NUMBER(TYPE) \ |
TaylorGy | 0:79de700849c5 | 220 | *value = json_child->value##TYPE; \ |
TaylorGy | 0:79de700849c5 | 221 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 222 | return 0; \ |
TaylorGy | 0:79de700849c5 | 223 | } |
TaylorGy | 0:79de700849c5 | 224 | |
TaylorGy | 0:79de700849c5 | 225 | #define UNPACK_SAVE_DATA_TYPE23_END_STRING() \ |
TaylorGy | 0:79de700849c5 | 226 | valuestring = json_child->valuestring; \ |
TaylorGy | 0:79de700849c5 | 227 | *value = (char*)malloc(strlen(valuestring)+1); \ |
TaylorGy | 0:79de700849c5 | 228 | memcpy((char*)(*value), valuestring, strlen(valuestring)+1); \ |
TaylorGy | 0:79de700849c5 | 229 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 230 | return 0; \ |
TaylorGy | 0:79de700849c5 | 231 | } |
TaylorGy | 0:79de700849c5 | 232 | |
TaylorGy | 0:79de700849c5 | 233 | #define MKFUN_UNPACK_SAVE_DATA_TYPE3(TYPE, NAME, MED) \ |
TaylorGy | 0:79de700849c5 | 234 | static int32 UnpackSavedataType3##NAME(EdpPacket* pkg, \ |
TaylorGy | 0:79de700849c5 | 235 | char** ds_id, \ |
TaylorGy | 0:79de700849c5 | 236 | TYPE* value) \ |
TaylorGy | 0:79de700849c5 | 237 | { \ |
TaylorGy | 0:79de700849c5 | 238 | cJSON* json_obj = NULL; \ |
TaylorGy | 0:79de700849c5 | 239 | cJSON* json_child = NULL; \ |
TaylorGy | 0:79de700849c5 | 240 | char* valuestring = NULL; \ |
TaylorGy | 0:79de700849c5 | 241 | size_t len = 0; \ |
TaylorGy | 0:79de700849c5 | 242 | \ |
TaylorGy | 0:79de700849c5 | 243 | if(0 != UnpackSavedataJson(pkg, &json_obj)) \ |
TaylorGy | 0:79de700849c5 | 244 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 245 | \ |
TaylorGy | 0:79de700849c5 | 246 | json_child = json_obj->child; \ |
TaylorGy | 0:79de700849c5 | 247 | if (!json_child || !json_child->string){ \ |
TaylorGy | 0:79de700849c5 | 248 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 249 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 250 | } \ |
TaylorGy | 0:79de700849c5 | 251 | \ |
TaylorGy | 0:79de700849c5 | 252 | len = strlen(json_child->string) + 1; \ |
TaylorGy | 0:79de700849c5 | 253 | *ds_id = (char*)malloc(len); \ |
TaylorGy | 0:79de700849c5 | 254 | memcpy(*ds_id, json_child->string, len); \ |
TaylorGy | 0:79de700849c5 | 255 | \ |
TaylorGy | 0:79de700849c5 | 256 | json_child = json_child->child; \ |
TaylorGy | 0:79de700849c5 | 257 | if (!json_child){ \ |
TaylorGy | 0:79de700849c5 | 258 | cJSON_Delete(json_obj); \ |
TaylorGy | 0:79de700849c5 | 259 | return ERR_UNPACK_SAVED_JSON; \ |
TaylorGy | 0:79de700849c5 | 260 | } |
TaylorGy | 0:79de700849c5 | 261 | |
TaylorGy | 0:79de700849c5 | 262 | |
TaylorGy | 0:79de700849c5 | 263 | static void FormatAt(char* buffer, int len, time_t now) |
TaylorGy | 0:79de700849c5 | 264 | { |
TaylorGy | 0:79de700849c5 | 265 | struct tm timinfo; |
TaylorGy | 0:79de700849c5 | 266 | |
TaylorGy | 0:79de700849c5 | 267 | if (now == 0){ |
TaylorGy | 0:79de700849c5 | 268 | now = 0; //time(0); |
TaylorGy | 0:79de700849c5 | 269 | } |
TaylorGy | 0:79de700849c5 | 270 | |
TaylorGy | 0:79de700849c5 | 271 | #if defined(linux) || defined(__linux) || defined(__linux__) |
TaylorGy | 0:79de700849c5 | 272 | strftime(buffer, len, "%F %T", localtime_r(&now, &timinfo)); |
TaylorGy | 0:79de700849c5 | 273 | #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
TaylorGy | 0:79de700849c5 | 274 | localtime_s(&timinfo, &now); |
TaylorGy | 0:79de700849c5 | 275 | strftime(buffer, len, "%F %T", &timinfo); |
TaylorGy | 0:79de700849c5 | 276 | #endif |
TaylorGy | 0:79de700849c5 | 277 | } |
TaylorGy | 0:79de700849c5 | 278 | |
TaylorGy | 0:79de700849c5 | 279 | /*---------------------------------------------------------------------------*/ |
TaylorGy | 0:79de700849c5 | 280 | Buffer* NewBuffer() |
TaylorGy | 0:79de700849c5 | 281 | { |
TaylorGy | 0:79de700849c5 | 282 | Buffer* buf = (Buffer*)malloc(sizeof(Buffer)); |
TaylorGy | 0:79de700849c5 | 283 | buf->_data = (uint8*)malloc(sizeof(uint8) * BUFFER_SIZE); |
TaylorGy | 0:79de700849c5 | 284 | buf->_write_pos = 0; |
TaylorGy | 0:79de700849c5 | 285 | buf->_read_pos = 0; |
TaylorGy | 0:79de700849c5 | 286 | buf->_capacity = BUFFER_SIZE; |
TaylorGy | 0:79de700849c5 | 287 | return buf; |
TaylorGy | 0:79de700849c5 | 288 | } |
TaylorGy | 0:79de700849c5 | 289 | |
TaylorGy | 0:79de700849c5 | 290 | void DeleteBuffer(Buffer** buf) |
TaylorGy | 0:79de700849c5 | 291 | { |
TaylorGy | 0:79de700849c5 | 292 | uint8* pdata = (*buf)->_data; |
TaylorGy | 0:79de700849c5 | 293 | free(pdata); |
TaylorGy | 0:79de700849c5 | 294 | free(*buf); |
TaylorGy | 0:79de700849c5 | 295 | *buf = 0; |
TaylorGy | 0:79de700849c5 | 296 | } |
TaylorGy | 0:79de700849c5 | 297 | |
TaylorGy | 0:79de700849c5 | 298 | int32 CheckCapacity(Buffer* buf, uint32 len) |
TaylorGy | 0:79de700849c5 | 299 | { |
TaylorGy | 0:79de700849c5 | 300 | uint32 cap_len = buf->_capacity; |
TaylorGy | 0:79de700849c5 | 301 | int32 flag = 0; |
TaylorGy | 0:79de700849c5 | 302 | uint8* pdata = NULL; |
TaylorGy | 0:79de700849c5 | 303 | |
TaylorGy | 0:79de700849c5 | 304 | while (cap_len - buf->_write_pos < len) /* remain len < len */ |
TaylorGy | 0:79de700849c5 | 305 | { |
TaylorGy | 0:79de700849c5 | 306 | cap_len = cap_len << 1; |
TaylorGy | 0:79de700849c5 | 307 | if (++flag > 32) |
TaylorGy | 0:79de700849c5 | 308 | break; /* overflow */ |
TaylorGy | 0:79de700849c5 | 309 | } |
TaylorGy | 0:79de700849c5 | 310 | if (flag > 32) |
TaylorGy | 0:79de700849c5 | 311 | return -1; |
TaylorGy | 0:79de700849c5 | 312 | if (cap_len > buf->_capacity) |
TaylorGy | 0:79de700849c5 | 313 | { |
TaylorGy | 0:79de700849c5 | 314 | pdata = (uint8*)malloc(sizeof(uint8) * cap_len); |
TaylorGy | 0:79de700849c5 | 315 | memcpy(pdata, buf->_data, buf->_write_pos); |
TaylorGy | 0:79de700849c5 | 316 | free(buf->_data); |
TaylorGy | 0:79de700849c5 | 317 | buf->_data = pdata; |
TaylorGy | 0:79de700849c5 | 318 | buf->_capacity = cap_len; |
TaylorGy | 0:79de700849c5 | 319 | } |
TaylorGy | 0:79de700849c5 | 320 | return 0; |
TaylorGy | 0:79de700849c5 | 321 | } |
TaylorGy | 0:79de700849c5 | 322 | |
TaylorGy | 0:79de700849c5 | 323 | /*---------------------------------------------------------------------------*/ |
TaylorGy | 0:79de700849c5 | 324 | int32 ReadByte(EdpPacket* pkg, uint8* val) |
TaylorGy | 0:79de700849c5 | 325 | { |
TaylorGy | 0:79de700849c5 | 326 | if (pkg->_read_pos+1 > pkg->_write_pos) |
TaylorGy | 0:79de700849c5 | 327 | return -1; |
TaylorGy | 0:79de700849c5 | 328 | *val = pkg->_data[pkg->_read_pos]; |
TaylorGy | 0:79de700849c5 | 329 | pkg->_read_pos += 1; |
TaylorGy | 0:79de700849c5 | 330 | return 0; |
TaylorGy | 0:79de700849c5 | 331 | } |
TaylorGy | 0:79de700849c5 | 332 | |
TaylorGy | 0:79de700849c5 | 333 | int32 ReadBytes(EdpPacket* pkg, uint8** val, uint32 count) |
TaylorGy | 0:79de700849c5 | 334 | { |
TaylorGy | 0:79de700849c5 | 335 | if (pkg->_read_pos+count > pkg->_write_pos) |
TaylorGy | 0:79de700849c5 | 336 | return -1; |
TaylorGy | 0:79de700849c5 | 337 | *val = (uint8*)malloc(sizeof(uint8) * count); |
TaylorGy | 0:79de700849c5 | 338 | memcpy(*val, pkg->_data + pkg->_read_pos, count); |
TaylorGy | 0:79de700849c5 | 339 | pkg->_read_pos += count; |
TaylorGy | 0:79de700849c5 | 340 | return 0; |
TaylorGy | 0:79de700849c5 | 341 | } |
TaylorGy | 0:79de700849c5 | 342 | |
TaylorGy | 0:79de700849c5 | 343 | int32 ReadUint16(EdpPacket* pkg, uint16* val) |
TaylorGy | 0:79de700849c5 | 344 | { |
TaylorGy | 0:79de700849c5 | 345 | uint8 msb, lsb; |
TaylorGy | 0:79de700849c5 | 346 | if (pkg->_read_pos+2 > pkg->_write_pos) |
TaylorGy | 0:79de700849c5 | 347 | return -1; |
TaylorGy | 0:79de700849c5 | 348 | msb = pkg->_data[pkg->_read_pos]; |
TaylorGy | 0:79de700849c5 | 349 | pkg->_read_pos++; |
TaylorGy | 0:79de700849c5 | 350 | lsb = pkg->_data[pkg->_read_pos]; |
TaylorGy | 0:79de700849c5 | 351 | pkg->_read_pos++; |
TaylorGy | 0:79de700849c5 | 352 | *val = (msb<<8) + lsb; |
TaylorGy | 0:79de700849c5 | 353 | return 0; |
TaylorGy | 0:79de700849c5 | 354 | } |
TaylorGy | 0:79de700849c5 | 355 | |
TaylorGy | 0:79de700849c5 | 356 | int32 ReadUint32(EdpPacket* pkg, uint32* val) |
TaylorGy | 0:79de700849c5 | 357 | { |
TaylorGy | 0:79de700849c5 | 358 | int32 i = 0; |
TaylorGy | 0:79de700849c5 | 359 | uint32 tmpval = 0; |
TaylorGy | 0:79de700849c5 | 360 | if (pkg->_read_pos+4 > pkg->_write_pos) |
TaylorGy | 0:79de700849c5 | 361 | return -1; |
TaylorGy | 0:79de700849c5 | 362 | while (i++ < 4) { |
TaylorGy | 0:79de700849c5 | 363 | tmpval = (tmpval << 8) | (pkg->_data[pkg->_read_pos]); |
TaylorGy | 0:79de700849c5 | 364 | pkg->_read_pos++; |
TaylorGy | 0:79de700849c5 | 365 | } |
TaylorGy | 0:79de700849c5 | 366 | *val = tmpval; |
TaylorGy | 0:79de700849c5 | 367 | return 0; |
TaylorGy | 0:79de700849c5 | 368 | } |
TaylorGy | 0:79de700849c5 | 369 | |
TaylorGy | 0:79de700849c5 | 370 | int32 ReadStr(EdpPacket* pkg, char** val) |
TaylorGy | 0:79de700849c5 | 371 | { |
TaylorGy | 0:79de700849c5 | 372 | uint16 len = 0; |
TaylorGy | 0:79de700849c5 | 373 | int rc = 0; |
TaylorGy | 0:79de700849c5 | 374 | /* read str len */ |
TaylorGy | 0:79de700849c5 | 375 | rc = ReadUint16(pkg, &len); |
TaylorGy | 0:79de700849c5 | 376 | if (rc) |
TaylorGy | 0:79de700849c5 | 377 | return rc; |
TaylorGy | 0:79de700849c5 | 378 | if (pkg->_read_pos+len > pkg->_write_pos) |
TaylorGy | 0:79de700849c5 | 379 | return -1; |
TaylorGy | 0:79de700849c5 | 380 | /* copy str val */ |
TaylorGy | 0:79de700849c5 | 381 | *val = (char*)malloc(sizeof(char) * (len + 1)); |
TaylorGy | 0:79de700849c5 | 382 | memset(*val, 0, len+1); |
TaylorGy | 0:79de700849c5 | 383 | strncpy(*val, (const char *)(pkg->_data + pkg->_read_pos), len); |
TaylorGy | 0:79de700849c5 | 384 | pkg->_read_pos += len; |
TaylorGy | 0:79de700849c5 | 385 | return 0; |
TaylorGy | 0:79de700849c5 | 386 | } |
TaylorGy | 0:79de700849c5 | 387 | |
TaylorGy | 0:79de700849c5 | 388 | int32 ReadRemainlen(EdpPacket* pkg, uint32* len_val) |
TaylorGy | 0:79de700849c5 | 389 | { |
TaylorGy | 0:79de700849c5 | 390 | uint32 multiplier = 1; |
TaylorGy | 0:79de700849c5 | 391 | uint32 len_len = 0; |
TaylorGy | 0:79de700849c5 | 392 | uint8 onebyte = 0; |
TaylorGy | 0:79de700849c5 | 393 | int32 rc; |
TaylorGy | 0:79de700849c5 | 394 | *len_val = 0; |
TaylorGy | 0:79de700849c5 | 395 | do { |
TaylorGy | 0:79de700849c5 | 396 | rc = ReadByte(pkg, &onebyte); |
TaylorGy | 0:79de700849c5 | 397 | if (rc) |
TaylorGy | 0:79de700849c5 | 398 | return rc; |
TaylorGy | 0:79de700849c5 | 399 | |
TaylorGy | 0:79de700849c5 | 400 | *len_val += (onebyte & 0x7f) * multiplier; |
TaylorGy | 0:79de700849c5 | 401 | multiplier *= 0x80; |
TaylorGy | 0:79de700849c5 | 402 | |
TaylorGy | 0:79de700849c5 | 403 | len_len++; |
TaylorGy | 0:79de700849c5 | 404 | if (len_len > 4) { |
TaylorGy | 0:79de700849c5 | 405 | return -1;/*len of len more than 4;*/ |
TaylorGy | 0:79de700849c5 | 406 | } |
TaylorGy | 0:79de700849c5 | 407 | } while((onebyte & 0x80) != 0); |
TaylorGy | 0:79de700849c5 | 408 | return 0; |
TaylorGy | 0:79de700849c5 | 409 | } |
TaylorGy | 0:79de700849c5 | 410 | |
TaylorGy | 0:79de700849c5 | 411 | /*---------------------------------------------------------------------------*/ |
TaylorGy | 0:79de700849c5 | 412 | int32 WriteByte(Buffer* buf, uint8 byte) |
TaylorGy | 0:79de700849c5 | 413 | { |
TaylorGy | 0:79de700849c5 | 414 | assert(buf->_read_pos == 0); |
TaylorGy | 0:79de700849c5 | 415 | if (CheckCapacity(buf, 1)) |
TaylorGy | 0:79de700849c5 | 416 | return -1; |
TaylorGy | 0:79de700849c5 | 417 | buf->_data[buf->_write_pos] = byte; |
TaylorGy | 0:79de700849c5 | 418 | buf->_write_pos++; |
TaylorGy | 0:79de700849c5 | 419 | return 0; |
TaylorGy | 0:79de700849c5 | 420 | } |
TaylorGy | 0:79de700849c5 | 421 | |
TaylorGy | 0:79de700849c5 | 422 | int32 WriteBytes(Buffer* buf, const void* bytes, uint32 count) |
TaylorGy | 0:79de700849c5 | 423 | { |
TaylorGy | 0:79de700849c5 | 424 | assert(buf->_read_pos == 0); |
TaylorGy | 0:79de700849c5 | 425 | if (CheckCapacity(buf, count)) |
TaylorGy | 0:79de700849c5 | 426 | return -1; |
TaylorGy | 0:79de700849c5 | 427 | memcpy(buf->_data + buf->_write_pos, bytes, count); |
TaylorGy | 0:79de700849c5 | 428 | buf->_write_pos += count; |
TaylorGy | 0:79de700849c5 | 429 | return 0; |
TaylorGy | 0:79de700849c5 | 430 | } |
TaylorGy | 0:79de700849c5 | 431 | |
TaylorGy | 0:79de700849c5 | 432 | int32 WriteUint16(Buffer* buf, uint16 val) |
TaylorGy | 0:79de700849c5 | 433 | { |
TaylorGy | 0:79de700849c5 | 434 | assert(buf->_read_pos == 0); |
TaylorGy | 0:79de700849c5 | 435 | return WriteByte(buf, MOSQ_MSB(val)) |
TaylorGy | 0:79de700849c5 | 436 | || WriteByte(buf, MOSQ_LSB(val)); |
TaylorGy | 0:79de700849c5 | 437 | } |
TaylorGy | 0:79de700849c5 | 438 | |
TaylorGy | 0:79de700849c5 | 439 | int32 WriteUint32(Buffer* buf, uint32 val) |
TaylorGy | 0:79de700849c5 | 440 | { |
TaylorGy | 0:79de700849c5 | 441 | assert(buf->_read_pos == 0); |
TaylorGy | 0:79de700849c5 | 442 | return WriteByte(buf, (val >> 24) & 0x00FF) |
TaylorGy | 0:79de700849c5 | 443 | || WriteByte(buf, (val >> 16) & 0x00FF) |
TaylorGy | 0:79de700849c5 | 444 | || WriteByte(buf, (val >> 8) & 0x00FF) |
TaylorGy | 0:79de700849c5 | 445 | || WriteByte(buf, (val) & 0x00FF); |
TaylorGy | 0:79de700849c5 | 446 | } |
TaylorGy | 0:79de700849c5 | 447 | |
TaylorGy | 0:79de700849c5 | 448 | int32 WriteStr(Buffer* buf, const char *str) |
TaylorGy | 0:79de700849c5 | 449 | { |
TaylorGy | 0:79de700849c5 | 450 | uint16 length = 0; |
TaylorGy | 0:79de700849c5 | 451 | assert(buf->_read_pos == 0); |
TaylorGy | 0:79de700849c5 | 452 | length = strlen(str); |
TaylorGy | 0:79de700849c5 | 453 | return WriteUint16(buf, length) |
TaylorGy | 0:79de700849c5 | 454 | || WriteBytes(buf, str, length); |
TaylorGy | 0:79de700849c5 | 455 | } |
TaylorGy | 0:79de700849c5 | 456 | |
TaylorGy | 0:79de700849c5 | 457 | int32 WriteRemainlen(Buffer* buf, uint32 len_val) |
TaylorGy | 0:79de700849c5 | 458 | { |
TaylorGy | 0:79de700849c5 | 459 | uint32 remaining_length = len_val; |
TaylorGy | 0:79de700849c5 | 460 | int32 remaining_count = 0; |
TaylorGy | 0:79de700849c5 | 461 | uint8 byte = 0; |
TaylorGy | 0:79de700849c5 | 462 | |
TaylorGy | 0:79de700849c5 | 463 | assert(buf->_read_pos == 0); |
TaylorGy | 0:79de700849c5 | 464 | |
TaylorGy | 0:79de700849c5 | 465 | do { |
TaylorGy | 0:79de700849c5 | 466 | byte = remaining_length % 128; |
TaylorGy | 0:79de700849c5 | 467 | remaining_length = remaining_length / 128; |
TaylorGy | 0:79de700849c5 | 468 | /* If there are more digits to encode, set the top bit of this digit */ |
TaylorGy | 0:79de700849c5 | 469 | if (remaining_length > 0) { |
TaylorGy | 0:79de700849c5 | 470 | byte = byte | 0x80; |
TaylorGy | 0:79de700849c5 | 471 | } |
TaylorGy | 0:79de700849c5 | 472 | buf->_data[buf->_write_pos++] = byte; |
TaylorGy | 0:79de700849c5 | 473 | remaining_count++; |
TaylorGy | 0:79de700849c5 | 474 | }while(remaining_length > 0 && remaining_count < 5); |
TaylorGy | 0:79de700849c5 | 475 | assert(remaining_count != 5); |
TaylorGy | 0:79de700849c5 | 476 | return 0; |
TaylorGy | 0:79de700849c5 | 477 | } |
TaylorGy | 0:79de700849c5 | 478 | |
TaylorGy | 0:79de700849c5 | 479 | /*---------------------------------------------------------------------------*/ |
TaylorGy | 0:79de700849c5 | 480 | /* connect1 (C->S): devid + apikey */ |
TaylorGy | 0:79de700849c5 | 481 | EdpPacket* PacketConnect1(const char* devid, const char* auth_key) |
TaylorGy | 0:79de700849c5 | 482 | { |
TaylorGy | 0:79de700849c5 | 483 | EdpPacket* pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 484 | uint32 remainlen; |
TaylorGy | 0:79de700849c5 | 485 | /* msg type */ |
TaylorGy | 0:79de700849c5 | 486 | WriteByte(pkg, CONNREQ); |
TaylorGy | 0:79de700849c5 | 487 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 488 | remainlen = (2+3)+1+1+2+(2+strlen(devid))+(2+strlen(auth_key)); |
TaylorGy | 0:79de700849c5 | 489 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 490 | /* protocol desc */ |
TaylorGy | 0:79de700849c5 | 491 | WriteStr(pkg, PROTOCOL_NAME); |
TaylorGy | 0:79de700849c5 | 492 | /* protocol version */ |
TaylorGy | 0:79de700849c5 | 493 | WriteByte(pkg, PROTOCOL_VERSION); |
TaylorGy | 0:79de700849c5 | 494 | /* connect flag */ |
TaylorGy | 0:79de700849c5 | 495 | WriteByte(pkg, 0x40); |
TaylorGy | 0:79de700849c5 | 496 | /* keep time */ |
TaylorGy | 0:79de700849c5 | 497 | WriteUint16(pkg, 0x0080); |
TaylorGy | 0:79de700849c5 | 498 | /* DEVID */ |
TaylorGy | 0:79de700849c5 | 499 | WriteStr(pkg, devid); |
TaylorGy | 0:79de700849c5 | 500 | /* auth key */ |
TaylorGy | 0:79de700849c5 | 501 | WriteStr(pkg, auth_key); |
TaylorGy | 0:79de700849c5 | 502 | return pkg; |
TaylorGy | 0:79de700849c5 | 503 | } |
TaylorGy | 0:79de700849c5 | 504 | |
TaylorGy | 0:79de700849c5 | 505 | /* connect2 (C->S): userid + auth_info */ |
TaylorGy | 0:79de700849c5 | 506 | EdpPacket* PacketConnect2(const char* userid, const char* auth_info) |
TaylorGy | 0:79de700849c5 | 507 | { |
TaylorGy | 0:79de700849c5 | 508 | EdpPacket* pkg = NULL; |
TaylorGy | 0:79de700849c5 | 509 | uint32 remainlen; |
TaylorGy | 0:79de700849c5 | 510 | |
TaylorGy | 0:79de700849c5 | 511 | pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 512 | /* msg type */ |
TaylorGy | 0:79de700849c5 | 513 | WriteByte(pkg, CONNREQ); |
TaylorGy | 0:79de700849c5 | 514 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 515 | remainlen = (2+3)+1+1+2+2+(2+strlen(userid))+(2+strlen(auth_info)); |
TaylorGy | 0:79de700849c5 | 516 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 517 | /* protocol desc */ |
TaylorGy | 0:79de700849c5 | 518 | WriteStr(pkg, PROTOCOL_NAME); |
TaylorGy | 0:79de700849c5 | 519 | /* protocol version */ |
TaylorGy | 0:79de700849c5 | 520 | WriteByte(pkg, PROTOCOL_VERSION); |
TaylorGy | 0:79de700849c5 | 521 | /* connect flag */ |
TaylorGy | 0:79de700849c5 | 522 | WriteByte(pkg, 0xC0); |
TaylorGy | 0:79de700849c5 | 523 | /* keep time */ |
TaylorGy | 0:79de700849c5 | 524 | WriteUint16(pkg, 0x0080); |
TaylorGy | 0:79de700849c5 | 525 | /* devid */ |
TaylorGy | 0:79de700849c5 | 526 | WriteByte(pkg, 0x00); |
TaylorGy | 0:79de700849c5 | 527 | WriteByte(pkg, 0x00); |
TaylorGy | 0:79de700849c5 | 528 | /* USERID */ |
TaylorGy | 0:79de700849c5 | 529 | WriteStr(pkg, userid); |
TaylorGy | 0:79de700849c5 | 530 | /* auth info */ |
TaylorGy | 0:79de700849c5 | 531 | WriteStr(pkg, auth_info); |
TaylorGy | 0:79de700849c5 | 532 | return pkg; |
TaylorGy | 0:79de700849c5 | 533 | } |
TaylorGy | 0:79de700849c5 | 534 | |
TaylorGy | 0:79de700849c5 | 535 | /* push_data (C->S) */ |
TaylorGy | 0:79de700849c5 | 536 | EdpPacket* PacketPushdata(const char* dst_devid, const char* data, uint32 data_len) |
TaylorGy | 0:79de700849c5 | 537 | { |
TaylorGy | 0:79de700849c5 | 538 | EdpPacket* pkg = NULL; |
TaylorGy | 0:79de700849c5 | 539 | uint32 remainlen; |
TaylorGy | 0:79de700849c5 | 540 | |
TaylorGy | 0:79de700849c5 | 541 | pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 542 | /* msg type */ |
TaylorGy | 0:79de700849c5 | 543 | WriteByte(pkg, PUSHDATA); |
TaylorGy | 0:79de700849c5 | 544 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 545 | remainlen = (2+strlen(dst_devid))+data_len; |
TaylorGy | 0:79de700849c5 | 546 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 547 | /* dst devid */ |
TaylorGy | 0:79de700849c5 | 548 | WriteStr(pkg, dst_devid); |
TaylorGy | 0:79de700849c5 | 549 | /* data */ |
TaylorGy | 0:79de700849c5 | 550 | WriteBytes(pkg, data, data_len); |
TaylorGy | 0:79de700849c5 | 551 | return pkg; |
TaylorGy | 0:79de700849c5 | 552 | } |
TaylorGy | 0:79de700849c5 | 553 | |
TaylorGy | 0:79de700849c5 | 554 | /* sava_data (C->S) */ |
TaylorGy | 0:79de700849c5 | 555 | EdpPacket* PacketSavedataJson(const char* dst_devid, cJSON* json_obj, int type) |
TaylorGy | 0:79de700849c5 | 556 | { |
TaylorGy | 0:79de700849c5 | 557 | EdpPacket* pkg = NULL; |
TaylorGy | 0:79de700849c5 | 558 | uint32 remainlen = 0; |
TaylorGy | 0:79de700849c5 | 559 | char* json_out = NULL; |
TaylorGy | 0:79de700849c5 | 560 | uint32 json_len = 0; |
TaylorGy | 0:79de700849c5 | 561 | |
TaylorGy | 0:79de700849c5 | 562 | pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 563 | remainlen; |
TaylorGy | 0:79de700849c5 | 564 | json_out = cJSON_Print(json_obj); |
TaylorGy | 0:79de700849c5 | 565 | json_len = strlen(json_out); |
TaylorGy | 0:79de700849c5 | 566 | |
TaylorGy | 0:79de700849c5 | 567 | /* msg type */ |
TaylorGy | 0:79de700849c5 | 568 | WriteByte(pkg, SAVEDATA); |
TaylorGy | 0:79de700849c5 | 569 | if (dst_devid) |
TaylorGy | 0:79de700849c5 | 570 | { |
TaylorGy | 0:79de700849c5 | 571 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 572 | remainlen = 1+(2+strlen(dst_devid))+1+(2+json_len); |
TaylorGy | 0:79de700849c5 | 573 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 574 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 575 | WriteByte(pkg, 0x80); |
TaylorGy | 0:79de700849c5 | 576 | /* dst devid */ |
TaylorGy | 0:79de700849c5 | 577 | WriteStr(pkg, dst_devid); |
TaylorGy | 0:79de700849c5 | 578 | } |
TaylorGy | 0:79de700849c5 | 579 | else |
TaylorGy | 0:79de700849c5 | 580 | { |
TaylorGy | 0:79de700849c5 | 581 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 582 | remainlen = 1+1+(2+json_len); |
TaylorGy | 0:79de700849c5 | 583 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 584 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 585 | WriteByte(pkg, 0x00); |
TaylorGy | 0:79de700849c5 | 586 | } |
TaylorGy | 0:79de700849c5 | 587 | /* json flag */ |
TaylorGy | 0:79de700849c5 | 588 | WriteByte(pkg, type); |
TaylorGy | 0:79de700849c5 | 589 | /* json */ |
TaylorGy | 0:79de700849c5 | 590 | WriteStr(pkg, json_out); |
TaylorGy | 0:79de700849c5 | 591 | free(json_out); |
TaylorGy | 0:79de700849c5 | 592 | return pkg; |
TaylorGy | 0:79de700849c5 | 593 | } |
TaylorGy | 0:79de700849c5 | 594 | |
TaylorGy | 0:79de700849c5 | 595 | /* |
TaylorGy | 0:79de700849c5 | 596 | * 定义3种JSON格式的打包和解包函数, |
TaylorGy | 0:79de700849c5 | 597 | * 每种格式都支持int,double和char*三种类型 |
TaylorGy | 0:79de700849c5 | 598 | */ |
TaylorGy | 0:79de700849c5 | 599 | MKFUN_PACKET_SAVE_DATA_TYPE1(int, Int, Number) |
TaylorGy | 0:79de700849c5 | 600 | MKFUN_PACKET_SAVE_DATA_TYPE1(double, Double, Number) |
TaylorGy | 0:79de700849c5 | 601 | MKFUN_PACKET_SAVE_DATA_TYPE1(const char*, String, String) |
TaylorGy | 0:79de700849c5 | 602 | |
TaylorGy | 0:79de700849c5 | 603 | MKFUN_PACKET_SAVE_DATA_TYPE2(int, Int, Number) |
TaylorGy | 0:79de700849c5 | 604 | MKFUN_PACKET_SAVE_DATA_TYPE2(double, Double, Number) |
TaylorGy | 0:79de700849c5 | 605 | MKFUN_PACKET_SAVE_DATA_TYPE2(const char*, String, String) |
TaylorGy | 0:79de700849c5 | 606 | |
TaylorGy | 0:79de700849c5 | 607 | MKFUN_PACKET_SAVE_DATA_TYPE3(int, Int, Number) |
TaylorGy | 0:79de700849c5 | 608 | MKFUN_PACKET_SAVE_DATA_TYPE3(double, Double, Number) |
TaylorGy | 0:79de700849c5 | 609 | MKFUN_PACKET_SAVE_DATA_TYPE3(const char*, String, String) |
TaylorGy | 0:79de700849c5 | 610 | |
TaylorGy | 0:79de700849c5 | 611 | MKFUN_PACKET_SAVE_DATA(int, Int) |
TaylorGy | 0:79de700849c5 | 612 | MKFUN_PACKET_SAVE_DATA(double, Double) |
TaylorGy | 0:79de700849c5 | 613 | MKFUN_PACKET_SAVE_DATA(const char*, String) |
TaylorGy | 0:79de700849c5 | 614 | |
TaylorGy | 0:79de700849c5 | 615 | /* 解包函数 */ |
TaylorGy | 0:79de700849c5 | 616 | MKFUN_UNPACK_SAVE_DATA_TYPE1(int, Int, Number) |
TaylorGy | 0:79de700849c5 | 617 | UNPACK_SAVE_DATA_TYPE1_END_NUMBER(int) |
TaylorGy | 0:79de700849c5 | 618 | MKFUN_UNPACK_SAVE_DATA_TYPE1(double, Double, Number) |
TaylorGy | 0:79de700849c5 | 619 | UNPACK_SAVE_DATA_TYPE1_END_NUMBER(double) |
TaylorGy | 0:79de700849c5 | 620 | MKFUN_UNPACK_SAVE_DATA_TYPE1(char*, String, String) |
TaylorGy | 0:79de700849c5 | 621 | UNPACK_SAVE_DATA_TYPE1_END_STRING |
TaylorGy | 0:79de700849c5 | 622 | |
TaylorGy | 0:79de700849c5 | 623 | MKFUN_UNPACK_SAVE_DATA_TYPE2(int, Int, Number) |
TaylorGy | 0:79de700849c5 | 624 | UNPACK_SAVE_DATA_TYPE23_END_NUMBER(int) |
TaylorGy | 0:79de700849c5 | 625 | MKFUN_UNPACK_SAVE_DATA_TYPE2(double, Double, Number) |
TaylorGy | 0:79de700849c5 | 626 | UNPACK_SAVE_DATA_TYPE23_END_NUMBER(double) |
TaylorGy | 0:79de700849c5 | 627 | MKFUN_UNPACK_SAVE_DATA_TYPE2(char*, String, String) |
TaylorGy | 0:79de700849c5 | 628 | UNPACK_SAVE_DATA_TYPE23_END_STRING() |
TaylorGy | 0:79de700849c5 | 629 | |
TaylorGy | 0:79de700849c5 | 630 | MKFUN_UNPACK_SAVE_DATA_TYPE3(int, Int, Number) |
TaylorGy | 0:79de700849c5 | 631 | UNPACK_SAVE_DATA_TYPE23_END_NUMBER(int) |
TaylorGy | 0:79de700849c5 | 632 | MKFUN_UNPACK_SAVE_DATA_TYPE3(double, Double, Number) |
TaylorGy | 0:79de700849c5 | 633 | UNPACK_SAVE_DATA_TYPE23_END_NUMBER(double) |
TaylorGy | 0:79de700849c5 | 634 | MKFUN_UNPACK_SAVE_DATA_TYPE3(char*, String, String) |
TaylorGy | 0:79de700849c5 | 635 | UNPACK_SAVE_DATA_TYPE23_END_STRING() |
TaylorGy | 0:79de700849c5 | 636 | |
TaylorGy | 0:79de700849c5 | 637 | MKFUN_UNPACK_SAVE_DATA(int, Int) |
TaylorGy | 0:79de700849c5 | 638 | MKFUN_UNPACK_SAVE_DATA(double, Double) |
TaylorGy | 0:79de700849c5 | 639 | MKFUN_UNPACK_SAVE_DATA(char*, String) |
TaylorGy | 0:79de700849c5 | 640 | |
TaylorGy | 0:79de700849c5 | 641 | int32 UnpackSavedataAck(EdpPacket* pkg, char** json_ack){ |
TaylorGy | 0:79de700849c5 | 642 | uint32 remainlen = 0; |
TaylorGy | 0:79de700849c5 | 643 | uint8 flag = 0; |
TaylorGy | 0:79de700849c5 | 644 | uint16 json_len = 0; |
TaylorGy | 0:79de700849c5 | 645 | |
TaylorGy | 0:79de700849c5 | 646 | if (ReadRemainlen(pkg, &remainlen)) |
TaylorGy | 0:79de700849c5 | 647 | return ERR_UNPACK_SAVEDATA_ACK; |
TaylorGy | 0:79de700849c5 | 648 | |
TaylorGy | 0:79de700849c5 | 649 | if (ReadByte(pkg, &flag)) |
TaylorGy | 0:79de700849c5 | 650 | return ERR_UNPACK_SAVEDATA_ACK; |
TaylorGy | 0:79de700849c5 | 651 | |
TaylorGy | 0:79de700849c5 | 652 | if (ReadUint16(pkg, &json_len)) |
TaylorGy | 0:79de700849c5 | 653 | return ERR_UNPACK_SAVEDATA_ACK; |
TaylorGy | 0:79de700849c5 | 654 | |
TaylorGy | 0:79de700849c5 | 655 | if (ReadBytes(pkg, (uint8**)(json_ack), json_len)) |
TaylorGy | 0:79de700849c5 | 656 | return ERR_UNPACK_SAVEDATA_ACK; |
TaylorGy | 0:79de700849c5 | 657 | |
TaylorGy | 0:79de700849c5 | 658 | return 0; |
TaylorGy | 0:79de700849c5 | 659 | } |
TaylorGy | 0:79de700849c5 | 660 | |
TaylorGy | 0:79de700849c5 | 661 | /* sava_data bin (C->S) */ |
TaylorGy | 0:79de700849c5 | 662 | EdpPacket* PacketSavedataBin(const char* dst_devid, |
TaylorGy | 0:79de700849c5 | 663 | cJSON* desc_obj, const uint8* bin_data, uint32 bin_len) |
TaylorGy | 0:79de700849c5 | 664 | { |
TaylorGy | 0:79de700849c5 | 665 | EdpPacket* pkg = NULL; |
TaylorGy | 0:79de700849c5 | 666 | uint32 remainlen = 0; |
TaylorGy | 0:79de700849c5 | 667 | char* desc_out = NULL; |
TaylorGy | 0:79de700849c5 | 668 | uint32 desc_len = 0; |
TaylorGy | 0:79de700849c5 | 669 | |
TaylorGy | 0:79de700849c5 | 670 | /* check arguments */ |
TaylorGy | 0:79de700849c5 | 671 | desc_out = cJSON_Print(desc_obj); |
TaylorGy | 0:79de700849c5 | 672 | desc_len = strlen(desc_out); |
TaylorGy | 0:79de700849c5 | 673 | if (desc_len > (0x01 << 16) || bin_len > (3 * (0x01 << 20)) |
TaylorGy | 0:79de700849c5 | 674 | /* desc < 2^16 && bin_len < 3M*/ |
TaylorGy | 0:79de700849c5 | 675 | || cJSON_GetObjectItem(desc_obj, "ds_id") == 0) |
TaylorGy | 0:79de700849c5 | 676 | /* desc_obj MUST has ds_id */ |
TaylorGy | 0:79de700849c5 | 677 | { |
TaylorGy | 0:79de700849c5 | 678 | free(desc_out); |
TaylorGy | 0:79de700849c5 | 679 | return 0; |
TaylorGy | 0:79de700849c5 | 680 | } |
TaylorGy | 0:79de700849c5 | 681 | pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 682 | /* msg type */ |
TaylorGy | 0:79de700849c5 | 683 | WriteByte(pkg, SAVEDATA); |
TaylorGy | 0:79de700849c5 | 684 | if (dst_devid) |
TaylorGy | 0:79de700849c5 | 685 | { |
TaylorGy | 0:79de700849c5 | 686 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 687 | remainlen = 1+(2+strlen(dst_devid))+1+(2+desc_len)+(4+bin_len); |
TaylorGy | 0:79de700849c5 | 688 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 689 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 690 | WriteByte(pkg, 0x80); |
TaylorGy | 0:79de700849c5 | 691 | /* dst devid */ |
TaylorGy | 0:79de700849c5 | 692 | WriteStr(pkg, dst_devid); |
TaylorGy | 0:79de700849c5 | 693 | } |
TaylorGy | 0:79de700849c5 | 694 | else |
TaylorGy | 0:79de700849c5 | 695 | { |
TaylorGy | 0:79de700849c5 | 696 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 697 | remainlen = 1+1+(2+desc_len)+(4+bin_len); |
TaylorGy | 0:79de700849c5 | 698 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 699 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 700 | WriteByte(pkg, 0x00); |
TaylorGy | 0:79de700849c5 | 701 | } |
TaylorGy | 0:79de700849c5 | 702 | /* bin flag */ |
TaylorGy | 0:79de700849c5 | 703 | WriteByte(pkg, 0x02); |
TaylorGy | 0:79de700849c5 | 704 | /* desc */ |
TaylorGy | 0:79de700849c5 | 705 | WriteStr(pkg, desc_out); |
TaylorGy | 0:79de700849c5 | 706 | free(desc_out); |
TaylorGy | 0:79de700849c5 | 707 | /* bin data */ |
TaylorGy | 0:79de700849c5 | 708 | WriteUint32(pkg, bin_len); |
TaylorGy | 0:79de700849c5 | 709 | WriteBytes(pkg, bin_data, bin_len); |
TaylorGy | 0:79de700849c5 | 710 | return pkg; |
TaylorGy | 0:79de700849c5 | 711 | } |
TaylorGy | 0:79de700849c5 | 712 | |
TaylorGy | 0:79de700849c5 | 713 | EdpPacket* PacketSavedataBinStr(const char* dst_devid, |
TaylorGy | 0:79de700849c5 | 714 | const char* desc_str, const uint8* bin_data, uint32 bin_len) |
TaylorGy | 0:79de700849c5 | 715 | { |
TaylorGy | 0:79de700849c5 | 716 | EdpPacket* pkg = NULL; |
TaylorGy | 0:79de700849c5 | 717 | uint32 remainlen = 0; |
TaylorGy | 0:79de700849c5 | 718 | uint32 desc_len = 0; |
TaylorGy | 0:79de700849c5 | 719 | |
TaylorGy | 0:79de700849c5 | 720 | /* check arguments */ |
TaylorGy | 0:79de700849c5 | 721 | desc_len = strlen(desc_str); |
TaylorGy | 0:79de700849c5 | 722 | if (desc_len > (0x01 << 16) || bin_len > (3 * (0x01 << 20))) |
TaylorGy | 0:79de700849c5 | 723 | { /* desc < 2^16 && bin_len < 3M*/ |
TaylorGy | 0:79de700849c5 | 724 | return 0; |
TaylorGy | 0:79de700849c5 | 725 | } |
TaylorGy | 0:79de700849c5 | 726 | pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 727 | /* msg type */ |
TaylorGy | 0:79de700849c5 | 728 | WriteByte(pkg, SAVEDATA); |
TaylorGy | 0:79de700849c5 | 729 | if (dst_devid) |
TaylorGy | 0:79de700849c5 | 730 | { |
TaylorGy | 0:79de700849c5 | 731 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 732 | remainlen = 1+(2+strlen(dst_devid))+1+(2+desc_len)+(4+bin_len); |
TaylorGy | 0:79de700849c5 | 733 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 734 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 735 | WriteByte(pkg, 0x80); |
TaylorGy | 0:79de700849c5 | 736 | /* dst devid */ |
TaylorGy | 0:79de700849c5 | 737 | WriteStr(pkg, dst_devid); |
TaylorGy | 0:79de700849c5 | 738 | } |
TaylorGy | 0:79de700849c5 | 739 | else |
TaylorGy | 0:79de700849c5 | 740 | { |
TaylorGy | 0:79de700849c5 | 741 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 742 | remainlen = 1+1+(2+desc_len)+(4+bin_len); |
TaylorGy | 0:79de700849c5 | 743 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 744 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 745 | WriteByte(pkg, 0x00); |
TaylorGy | 0:79de700849c5 | 746 | } |
TaylorGy | 0:79de700849c5 | 747 | /* bin flag */ |
TaylorGy | 0:79de700849c5 | 748 | WriteByte(pkg, 0x02); |
TaylorGy | 0:79de700849c5 | 749 | /* desc */ |
TaylorGy | 0:79de700849c5 | 750 | WriteStr(pkg, desc_str); |
TaylorGy | 0:79de700849c5 | 751 | /* bin data */ |
TaylorGy | 0:79de700849c5 | 752 | WriteUint32(pkg, bin_len); |
TaylorGy | 0:79de700849c5 | 753 | WriteBytes(pkg, bin_data, bin_len); |
TaylorGy | 0:79de700849c5 | 754 | return pkg; |
TaylorGy | 0:79de700849c5 | 755 | } |
TaylorGy | 0:79de700849c5 | 756 | |
TaylorGy | 0:79de700849c5 | 757 | EdpPacket* PacketCmdResp(const char* cmdid, uint16 cmdid_len, |
TaylorGy | 0:79de700849c5 | 758 | const char* resp, uint32 resp_len) |
TaylorGy | 0:79de700849c5 | 759 | { |
TaylorGy | 0:79de700849c5 | 760 | EdpPacket* send_pkg = NULL; |
TaylorGy | 0:79de700849c5 | 761 | unsigned remainlen = 0; |
TaylorGy | 0:79de700849c5 | 762 | |
TaylorGy | 0:79de700849c5 | 763 | send_pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 764 | /* 6 = 2 + 4 = len(cmdid_len) + len(resp_len) */ |
TaylorGy | 0:79de700849c5 | 765 | remainlen = cmdid_len + resp_len + (resp_len ? 6 : 2); |
TaylorGy | 0:79de700849c5 | 766 | WriteByte(send_pkg, CMDRESP); |
TaylorGy | 0:79de700849c5 | 767 | WriteRemainlen(send_pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 768 | WriteUint16(send_pkg, cmdid_len); |
TaylorGy | 0:79de700849c5 | 769 | WriteBytes(send_pkg, cmdid, cmdid_len); |
TaylorGy | 0:79de700849c5 | 770 | if (resp_len){ |
TaylorGy | 0:79de700849c5 | 771 | WriteUint32(send_pkg, resp_len); |
TaylorGy | 0:79de700849c5 | 772 | WriteBytes(send_pkg, resp, resp_len); |
TaylorGy | 0:79de700849c5 | 773 | } |
TaylorGy | 0:79de700849c5 | 774 | return send_pkg; |
TaylorGy | 0:79de700849c5 | 775 | } |
TaylorGy | 0:79de700849c5 | 776 | |
TaylorGy | 0:79de700849c5 | 777 | /* ping (C->S) */ |
TaylorGy | 0:79de700849c5 | 778 | EdpPacket* PacketPing(void) |
TaylorGy | 0:79de700849c5 | 779 | { |
TaylorGy | 0:79de700849c5 | 780 | EdpPacket* pkg = NULL; |
TaylorGy | 0:79de700849c5 | 781 | |
TaylorGy | 0:79de700849c5 | 782 | pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 783 | /* msg type */ |
TaylorGy | 0:79de700849c5 | 784 | WriteByte(pkg, PINGREQ); |
TaylorGy | 0:79de700849c5 | 785 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 786 | WriteRemainlen(pkg, 0); |
TaylorGy | 0:79de700849c5 | 787 | return pkg; |
TaylorGy | 0:79de700849c5 | 788 | } |
TaylorGy | 0:79de700849c5 | 789 | |
TaylorGy | 0:79de700849c5 | 790 | /*---------------------------------------------------------------------------*/ |
TaylorGy | 0:79de700849c5 | 791 | /* recv stream to a edp packet (S->C) */ |
TaylorGy | 0:79de700849c5 | 792 | EdpPacket* GetEdpPacket(RecvBuffer* buf) |
TaylorGy | 0:79de700849c5 | 793 | { |
TaylorGy | 0:79de700849c5 | 794 | EdpPacket* pkg = NULL; |
TaylorGy | 0:79de700849c5 | 795 | int32 flag = 0; |
TaylorGy | 0:79de700849c5 | 796 | |
TaylorGy | 0:79de700849c5 | 797 | assert(buf->_read_pos == 0); |
TaylorGy | 0:79de700849c5 | 798 | flag = IsPkgComplete(buf); |
TaylorGy | 0:79de700849c5 | 799 | if (flag <= 0) |
TaylorGy | 0:79de700849c5 | 800 | return pkg; |
TaylorGy | 0:79de700849c5 | 801 | pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 802 | WriteBytes(pkg, buf->_data, flag); |
TaylorGy | 0:79de700849c5 | 803 | /* shrink buffer */ |
TaylorGy | 0:79de700849c5 | 804 | memmove(buf->_data, buf->_data + flag, buf->_write_pos - flag); |
TaylorGy | 0:79de700849c5 | 805 | buf->_write_pos -= flag; |
TaylorGy | 0:79de700849c5 | 806 | return pkg; |
TaylorGy | 0:79de700849c5 | 807 | } |
TaylorGy | 0:79de700849c5 | 808 | |
TaylorGy | 0:79de700849c5 | 809 | /* is the recv buffer has a complete edp packet? */ |
TaylorGy | 0:79de700849c5 | 810 | int32 IsPkgComplete(RecvBuffer* buf) |
TaylorGy | 0:79de700849c5 | 811 | { |
TaylorGy | 0:79de700849c5 | 812 | uint8* data = NULL; |
TaylorGy | 0:79de700849c5 | 813 | uint32 data_len = 0; |
TaylorGy | 0:79de700849c5 | 814 | uint32 multiplier = 1; |
TaylorGy | 0:79de700849c5 | 815 | uint32 len_val = 0; |
TaylorGy | 0:79de700849c5 | 816 | uint32 len_len = 1; |
TaylorGy | 0:79de700849c5 | 817 | uint8* pdigit = NULL; |
TaylorGy | 0:79de700849c5 | 818 | uint32 pkg_total_len = 0; |
TaylorGy | 0:79de700849c5 | 819 | |
TaylorGy | 0:79de700849c5 | 820 | data = buf->_data; |
TaylorGy | 0:79de700849c5 | 821 | data_len = buf->_write_pos; |
TaylorGy | 0:79de700849c5 | 822 | |
TaylorGy | 0:79de700849c5 | 823 | if (data_len <= 1) { |
TaylorGy | 0:79de700849c5 | 824 | return 0; /* continue receive */ |
TaylorGy | 0:79de700849c5 | 825 | } |
TaylorGy | 0:79de700849c5 | 826 | /* recevie remaining len */ |
TaylorGy | 0:79de700849c5 | 827 | pdigit = data; |
TaylorGy | 0:79de700849c5 | 828 | |
TaylorGy | 0:79de700849c5 | 829 | do { |
TaylorGy | 0:79de700849c5 | 830 | if (len_len > 4) { |
TaylorGy | 0:79de700849c5 | 831 | return -1; /* protocol error; */ |
TaylorGy | 0:79de700849c5 | 832 | } |
TaylorGy | 0:79de700849c5 | 833 | if (len_len > data_len - 1) { |
TaylorGy | 0:79de700849c5 | 834 | return 0; /* continue receive */ |
TaylorGy | 0:79de700849c5 | 835 | } |
TaylorGy | 0:79de700849c5 | 836 | len_len++; |
TaylorGy | 0:79de700849c5 | 837 | pdigit++; |
TaylorGy | 0:79de700849c5 | 838 | len_val += ((*pdigit) & 0x7f) * multiplier; |
TaylorGy | 0:79de700849c5 | 839 | multiplier *= 0x80; |
TaylorGy | 0:79de700849c5 | 840 | }while(((*pdigit) & 0x80) != 0); |
TaylorGy | 0:79de700849c5 | 841 | |
TaylorGy | 0:79de700849c5 | 842 | pkg_total_len = len_len + len_val; |
TaylorGy | 0:79de700849c5 | 843 | /* receive payload */ |
TaylorGy | 0:79de700849c5 | 844 | if (pkg_total_len <= (uint32)data_len){ |
TaylorGy | 0:79de700849c5 | 845 | #ifdef _DEBUG |
TaylorGy | 0:79de700849c5 | 846 | printf("a complete packet len:%d\n", pkg_total_len); |
TaylorGy | 0:79de700849c5 | 847 | #endif |
TaylorGy | 0:79de700849c5 | 848 | return pkg_total_len; /* all data for this pkg is read */ |
TaylorGy | 0:79de700849c5 | 849 | }else{ |
TaylorGy | 0:79de700849c5 | 850 | return 0; /* continue receive */ |
TaylorGy | 0:79de700849c5 | 851 | } |
TaylorGy | 0:79de700849c5 | 852 | } |
TaylorGy | 0:79de700849c5 | 853 | |
TaylorGy | 0:79de700849c5 | 854 | /* get edp packet type, client should use this type to invoke Unpack??? function */ |
TaylorGy | 0:79de700849c5 | 855 | uint8 EdpPacketType(EdpPacket* pkg) |
TaylorGy | 0:79de700849c5 | 856 | { |
TaylorGy | 0:79de700849c5 | 857 | uint8 mtype = 0x00; |
TaylorGy | 0:79de700849c5 | 858 | ReadByte(pkg, &mtype); |
TaylorGy | 0:79de700849c5 | 859 | return mtype; |
TaylorGy | 0:79de700849c5 | 860 | } |
TaylorGy | 0:79de700849c5 | 861 | |
TaylorGy | 0:79de700849c5 | 862 | /* connect_resp (S->C)*/ |
TaylorGy | 0:79de700849c5 | 863 | int32 UnpackConnectResp(EdpPacket* pkg) |
TaylorGy | 0:79de700849c5 | 864 | { |
TaylorGy | 0:79de700849c5 | 865 | uint8 flag, rtn; |
TaylorGy | 0:79de700849c5 | 866 | uint32 remainlen; |
TaylorGy | 0:79de700849c5 | 867 | if (ReadRemainlen(pkg, &remainlen)) |
TaylorGy | 0:79de700849c5 | 868 | return ERR_UNPACK_CONNRESP_REMAIN; |
TaylorGy | 0:79de700849c5 | 869 | if (ReadByte(pkg, &flag)) |
TaylorGy | 0:79de700849c5 | 870 | return ERR_UNPACK_CONNRESP_FLAG; |
TaylorGy | 0:79de700849c5 | 871 | if (ReadByte(pkg, &rtn)) |
TaylorGy | 0:79de700849c5 | 872 | return ERR_UNPACK_CONNRESP_RTN; |
TaylorGy | 0:79de700849c5 | 873 | assert(pkg->_read_pos == pkg->_write_pos); |
TaylorGy | 0:79de700849c5 | 874 | return (int32)rtn; |
TaylorGy | 0:79de700849c5 | 875 | } |
TaylorGy | 0:79de700849c5 | 876 | |
TaylorGy | 0:79de700849c5 | 877 | /* push_data (S->C) */ |
TaylorGy | 0:79de700849c5 | 878 | int32 UnpackPushdata(EdpPacket* pkg, char** src_devid, char** data, uint32* data_len) |
TaylorGy | 0:79de700849c5 | 879 | { |
TaylorGy | 0:79de700849c5 | 880 | uint32 remainlen; |
TaylorGy | 0:79de700849c5 | 881 | if (ReadRemainlen(pkg, &remainlen)) |
TaylorGy | 0:79de700849c5 | 882 | return ERR_UNPACK_PUSHD_REMAIN; |
TaylorGy | 0:79de700849c5 | 883 | if (ReadStr(pkg, src_devid)) |
TaylorGy | 0:79de700849c5 | 884 | return ERR_UNPACK_PUSHD_DEVID; |
TaylorGy | 0:79de700849c5 | 885 | remainlen -= (2 + strlen(*src_devid)); |
TaylorGy | 0:79de700849c5 | 886 | if (ReadBytes(pkg, (uint8**)data, remainlen)) |
TaylorGy | 0:79de700849c5 | 887 | return ERR_UNPACK_PUSHD_DATA; |
TaylorGy | 0:79de700849c5 | 888 | *data_len = remainlen; |
TaylorGy | 0:79de700849c5 | 889 | assert(pkg->_read_pos == pkg->_write_pos); |
TaylorGy | 0:79de700849c5 | 890 | return 0; |
TaylorGy | 0:79de700849c5 | 891 | } |
TaylorGy | 0:79de700849c5 | 892 | |
TaylorGy | 0:79de700849c5 | 893 | /* save_data (S->C) */ |
TaylorGy | 0:79de700849c5 | 894 | int32 UnpackSavedata(EdpPacket* pkg, char** src_devid, uint8* jb_flag) |
TaylorGy | 0:79de700849c5 | 895 | { |
TaylorGy | 0:79de700849c5 | 896 | uint32 remainlen; |
TaylorGy | 0:79de700849c5 | 897 | uint8 ta_flag; |
TaylorGy | 0:79de700849c5 | 898 | if (ReadRemainlen(pkg, &remainlen)) |
TaylorGy | 0:79de700849c5 | 899 | return ERR_UNPACK_SAVED_REMAIN; |
TaylorGy | 0:79de700849c5 | 900 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 901 | if (ReadByte(pkg, &ta_flag)) |
TaylorGy | 0:79de700849c5 | 902 | return ERR_UNPACK_SAVED_TANSFLAG; |
TaylorGy | 0:79de700849c5 | 903 | if (ta_flag == 0x80) |
TaylorGy | 0:79de700849c5 | 904 | { |
TaylorGy | 0:79de700849c5 | 905 | if (ReadStr(pkg, src_devid)) |
TaylorGy | 0:79de700849c5 | 906 | return ERR_UNPACK_SAVED_DEVID; |
TaylorGy | 0:79de700849c5 | 907 | } |
TaylorGy | 0:79de700849c5 | 908 | else |
TaylorGy | 0:79de700849c5 | 909 | { |
TaylorGy | 0:79de700849c5 | 910 | *src_devid = 0; |
TaylorGy | 0:79de700849c5 | 911 | } |
TaylorGy | 0:79de700849c5 | 912 | /* json or bin */ |
TaylorGy | 0:79de700849c5 | 913 | if (ReadByte(pkg, jb_flag)) |
TaylorGy | 0:79de700849c5 | 914 | return ERR_UNPACK_SAVED_DATAFLAG; |
TaylorGy | 0:79de700849c5 | 915 | return 0; |
TaylorGy | 0:79de700849c5 | 916 | } |
TaylorGy | 0:79de700849c5 | 917 | |
TaylorGy | 0:79de700849c5 | 918 | int32 UnpackSavedataJson(EdpPacket* pkg, cJSON** json_obj) |
TaylorGy | 0:79de700849c5 | 919 | { |
TaylorGy | 0:79de700849c5 | 920 | char* json_str; |
TaylorGy | 0:79de700849c5 | 921 | if (ReadStr(pkg, &json_str)) |
TaylorGy | 0:79de700849c5 | 922 | return ERR_UNPACK_SAVED_JSON; |
TaylorGy | 0:79de700849c5 | 923 | *json_obj = cJSON_Parse(json_str); |
TaylorGy | 0:79de700849c5 | 924 | free(json_str); |
TaylorGy | 0:79de700849c5 | 925 | if (*json_obj == 0) |
TaylorGy | 0:79de700849c5 | 926 | return ERR_UNPACK_SAVED_PARSEJSON; |
TaylorGy | 0:79de700849c5 | 927 | assert(pkg->_read_pos == pkg->_write_pos); |
TaylorGy | 0:79de700849c5 | 928 | return 0; |
TaylorGy | 0:79de700849c5 | 929 | } |
TaylorGy | 0:79de700849c5 | 930 | |
TaylorGy | 0:79de700849c5 | 931 | int32 UnpackSavedataBin(EdpPacket* pkg, cJSON** desc_obj, |
TaylorGy | 0:79de700849c5 | 932 | uint8** bin_data, uint32* bin_len) |
TaylorGy | 0:79de700849c5 | 933 | { |
TaylorGy | 0:79de700849c5 | 934 | char* desc_str; |
TaylorGy | 0:79de700849c5 | 935 | if (ReadStr(pkg, &desc_str)) |
TaylorGy | 0:79de700849c5 | 936 | return ERR_UNPACK_SAVED_BIN_DESC; |
TaylorGy | 0:79de700849c5 | 937 | *desc_obj = cJSON_Parse(desc_str); |
TaylorGy | 0:79de700849c5 | 938 | free(desc_str); |
TaylorGy | 0:79de700849c5 | 939 | if (*desc_obj == 0) |
TaylorGy | 0:79de700849c5 | 940 | return ERR_UNPACK_SAVED_PARSEDESC; |
TaylorGy | 0:79de700849c5 | 941 | if (ReadUint32(pkg, bin_len)) |
TaylorGy | 0:79de700849c5 | 942 | return ERR_UNPACK_SAVED_BINLEN; |
TaylorGy | 0:79de700849c5 | 943 | if (ReadBytes(pkg, bin_data, *bin_len)) |
TaylorGy | 0:79de700849c5 | 944 | return ERR_UNPACK_SAVED_BINDATA; |
TaylorGy | 0:79de700849c5 | 945 | assert(pkg->_read_pos == pkg->_write_pos); |
TaylorGy | 0:79de700849c5 | 946 | return 0; |
TaylorGy | 0:79de700849c5 | 947 | } |
TaylorGy | 0:79de700849c5 | 948 | |
TaylorGy | 0:79de700849c5 | 949 | int32 UnpackSavedataBinStr(EdpPacket* pkg, char** desc_str, |
TaylorGy | 0:79de700849c5 | 950 | uint8** bin_data, uint32* bin_len) |
TaylorGy | 0:79de700849c5 | 951 | { |
TaylorGy | 0:79de700849c5 | 952 | if (ReadStr(pkg, desc_str)) |
TaylorGy | 0:79de700849c5 | 953 | return ERR_UNPACK_SAVED_BIN_DESC; |
TaylorGy | 0:79de700849c5 | 954 | if (ReadUint32(pkg, bin_len)) |
TaylorGy | 0:79de700849c5 | 955 | return ERR_UNPACK_SAVED_BINLEN; |
TaylorGy | 0:79de700849c5 | 956 | if (ReadBytes(pkg, bin_data, *bin_len)) |
TaylorGy | 0:79de700849c5 | 957 | return ERR_UNPACK_SAVED_BINDATA; |
TaylorGy | 0:79de700849c5 | 958 | assert(pkg->_read_pos == pkg->_write_pos); |
TaylorGy | 0:79de700849c5 | 959 | return 0; |
TaylorGy | 0:79de700849c5 | 960 | } |
TaylorGy | 0:79de700849c5 | 961 | |
TaylorGy | 0:79de700849c5 | 962 | int32 UnpackCmdReq(EdpPacket* pkg, char** cmdid, uint16* cmdid_len, |
TaylorGy | 0:79de700849c5 | 963 | char** req, uint32* req_len) |
TaylorGy | 0:79de700849c5 | 964 | { |
TaylorGy | 0:79de700849c5 | 965 | uint32 remainlen; |
TaylorGy | 0:79de700849c5 | 966 | int rc; |
TaylorGy | 0:79de700849c5 | 967 | if (ReadRemainlen(pkg, &remainlen)) |
TaylorGy | 0:79de700849c5 | 968 | return ERR_UNPACK_CMDREQ; |
TaylorGy | 0:79de700849c5 | 969 | |
TaylorGy | 0:79de700849c5 | 970 | rc = ReadUint16(pkg, cmdid_len); |
TaylorGy | 0:79de700849c5 | 971 | if (rc) |
TaylorGy | 0:79de700849c5 | 972 | return rc; |
TaylorGy | 0:79de700849c5 | 973 | if (ReadBytes(pkg, (uint8**)cmdid, *cmdid_len)) |
TaylorGy | 0:79de700849c5 | 974 | return ERR_UNPACK_CMDREQ; |
TaylorGy | 0:79de700849c5 | 975 | |
TaylorGy | 0:79de700849c5 | 976 | rc = ReadUint32(pkg, req_len); |
TaylorGy | 0:79de700849c5 | 977 | if (rc) |
TaylorGy | 0:79de700849c5 | 978 | return rc; |
TaylorGy | 0:79de700849c5 | 979 | if (ReadBytes(pkg, (uint8**)req, *req_len)) |
TaylorGy | 0:79de700849c5 | 980 | return ERR_UNPACK_CMDREQ; |
TaylorGy | 0:79de700849c5 | 981 | |
TaylorGy | 0:79de700849c5 | 982 | assert(pkg->_read_pos == pkg->_write_pos); |
TaylorGy | 0:79de700849c5 | 983 | return 0; |
TaylorGy | 0:79de700849c5 | 984 | } |
TaylorGy | 0:79de700849c5 | 985 | |
TaylorGy | 0:79de700849c5 | 986 | /* ping_resp (S->C) */ |
TaylorGy | 0:79de700849c5 | 987 | int32 UnpackPingResp(EdpPacket* pkg) |
TaylorGy | 0:79de700849c5 | 988 | { |
TaylorGy | 0:79de700849c5 | 989 | uint32 remainlen; |
TaylorGy | 0:79de700849c5 | 990 | if (ReadRemainlen(pkg, &remainlen)) |
TaylorGy | 0:79de700849c5 | 991 | return ERR_UNPACK_PING_REMAIN; |
TaylorGy | 0:79de700849c5 | 992 | assert(pkg->_read_pos == pkg->_write_pos); |
TaylorGy | 0:79de700849c5 | 993 | return 0; |
TaylorGy | 0:79de700849c5 | 994 | } |
TaylorGy | 0:79de700849c5 | 995 | |
TaylorGy | 0:79de700849c5 | 996 | /* sava_data (C->S) */ |
TaylorGy | 0:79de700849c5 | 997 | EdpPacket* PacketSavedataSimpleString(const char* dst_devid, const char* input) |
TaylorGy | 0:79de700849c5 | 998 | { |
TaylorGy | 0:79de700849c5 | 999 | EdpPacket* pkg = NULL; |
TaylorGy | 0:79de700849c5 | 1000 | uint32 remainlen = 0; |
TaylorGy | 0:79de700849c5 | 1001 | uint32 input_len = 0; |
TaylorGy | 0:79de700849c5 | 1002 | |
TaylorGy | 0:79de700849c5 | 1003 | pkg = NewBuffer(); |
TaylorGy | 0:79de700849c5 | 1004 | input_len = strlen(input); |
TaylorGy | 0:79de700849c5 | 1005 | /* msg type */ |
TaylorGy | 0:79de700849c5 | 1006 | WriteByte(pkg, SAVEDATA); |
TaylorGy | 0:79de700849c5 | 1007 | if (dst_devid) |
TaylorGy | 0:79de700849c5 | 1008 | { |
TaylorGy | 0:79de700849c5 | 1009 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 1010 | remainlen = 1+(2+strlen(dst_devid))+1+(2+input_len); |
TaylorGy | 0:79de700849c5 | 1011 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 1012 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 1013 | WriteByte(pkg, 0x80); |
TaylorGy | 0:79de700849c5 | 1014 | /* dst devid */ |
TaylorGy | 0:79de700849c5 | 1015 | WriteStr(pkg, dst_devid); |
TaylorGy | 0:79de700849c5 | 1016 | } |
TaylorGy | 0:79de700849c5 | 1017 | else |
TaylorGy | 0:79de700849c5 | 1018 | { |
TaylorGy | 0:79de700849c5 | 1019 | /* remain len */ |
TaylorGy | 0:79de700849c5 | 1020 | remainlen = 1+1+(2+input_len); |
TaylorGy | 0:79de700849c5 | 1021 | WriteRemainlen(pkg, remainlen); |
TaylorGy | 0:79de700849c5 | 1022 | /* translate address flag */ |
TaylorGy | 0:79de700849c5 | 1023 | WriteByte(pkg, 0x00); |
TaylorGy | 0:79de700849c5 | 1024 | } |
TaylorGy | 0:79de700849c5 | 1025 | /* json flag */ |
TaylorGy | 0:79de700849c5 | 1026 | WriteByte(pkg, kTypeString); |
TaylorGy | 0:79de700849c5 | 1027 | /* json */ |
TaylorGy | 0:79de700849c5 | 1028 | WriteStr(pkg, input); |
TaylorGy | 0:79de700849c5 | 1029 | |
TaylorGy | 0:79de700849c5 | 1030 | return pkg; |
TaylorGy | 0:79de700849c5 | 1031 | } |
TaylorGy | 0:79de700849c5 | 1032 | |
TaylorGy | 0:79de700849c5 | 1033 | int32 UnpackSavedataSimpleString(EdpPacket* pkg, char** output) |
TaylorGy | 0:79de700849c5 | 1034 | { |
TaylorGy | 0:79de700849c5 | 1035 | if (ReadStr(pkg, output)) |
TaylorGy | 0:79de700849c5 | 1036 | return ERR_UNPACK_SAVED_JSON; |
TaylorGy | 0:79de700849c5 | 1037 | return 0; |
TaylorGy | 0:79de700849c5 | 1038 | } |
TaylorGy | 0:79de700849c5 | 1039 |