Axeda Corp
/
AxedaGo-ubloxC027
Axeda demo software for u-blox C027 (GSM)
AMMP/axSerializer.c@1:ff6d8adaf6b9, 2014-08-11 (annotated)
- Committer:
- AxedaCorp
- Date:
- Mon Aug 11 19:07:20 2014 +0000
- Revision:
- 1:ff6d8adaf6b9
- Parent:
- 0:a725e8eab383
Pointed to platform (prod)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:a725e8eab383 | 1 | /************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 2 | /* axSerializer.c */ |
AxedaCorp | 0:a725e8eab383 | 3 | /* �2013 Axeda Corporation */ |
AxedaCorp | 0:a725e8eab383 | 4 | /* */ |
AxedaCorp | 0:a725e8eab383 | 5 | /* Defines methods for transposing the data in domain objects into a serialized */ |
AxedaCorp | 0:a725e8eab383 | 6 | /* encoding for transmission to the Platform Endpoint. This is a platform independent*/ |
AxedaCorp | 0:a725e8eab383 | 7 | /* implementation that can be applied to any device that supports ANSI C. */ |
AxedaCorp | 0:a725e8eab383 | 8 | /* */ |
AxedaCorp | 0:a725e8eab383 | 9 | /************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 10 | #include "axSerializer.h" |
AxedaCorp | 0:a725e8eab383 | 11 | |
AxedaCorp | 0:a725e8eab383 | 12 | #define REG_ID 0 |
AxedaCorp | 0:a725e8eab383 | 13 | #define REG_MODEL_NUMBER 1 |
AxedaCorp | 0:a725e8eab383 | 14 | #define REG_SERIAL_NUMBER 2 |
AxedaCorp | 0:a725e8eab383 | 15 | #define REG_TENANT 3 |
AxedaCorp | 0:a725e8eab383 | 16 | #define REG_PING 4 |
AxedaCorp | 0:a725e8eab383 | 17 | #define REG_KEY 5 |
AxedaCorp | 0:a725e8eab383 | 18 | |
AxedaCorp | 0:a725e8eab383 | 19 | #define DATA_ALERTS 0 |
AxedaCorp | 0:a725e8eab383 | 20 | #define DATA_EVENTS 1 |
AxedaCorp | 0:a725e8eab383 | 21 | #define DATA_DATA 2 |
AxedaCorp | 0:a725e8eab383 | 22 | #define DATA_LOCATIONS 3 |
AxedaCorp | 0:a725e8eab383 | 23 | #define DATA_ITEMS 4 |
AxedaCorp | 0:a725e8eab383 | 24 | |
AxedaCorp | 0:a725e8eab383 | 25 | #define PKG_STATUS 0 |
AxedaCorp | 0:a725e8eab383 | 26 | #define PKG_ERROR 1 |
AxedaCorp | 0:a725e8eab383 | 27 | |
AxedaCorp | 0:a725e8eab383 | 28 | #define ALERT_SEV 0 |
AxedaCorp | 0:a725e8eab383 | 29 | #define ALERT_CAUSE 1 |
AxedaCorp | 0:a725e8eab383 | 30 | #define ALERT_REASON 2 |
AxedaCorp | 0:a725e8eab383 | 31 | |
AxedaCorp | 0:a725e8eab383 | 32 | #define COM_TIME 0 |
AxedaCorp | 0:a725e8eab383 | 33 | #define COM_PRIORITY 1 |
AxedaCorp | 0:a725e8eab383 | 34 | #define COM_NAME 2 |
AxedaCorp | 0:a725e8eab383 | 35 | #define COM_DESCRIPTION 3 |
AxedaCorp | 0:a725e8eab383 | 36 | |
AxedaCorp | 0:a725e8eab383 | 37 | //#define EVENT_NAME 1 |
AxedaCorp | 0:a725e8eab383 | 38 | //#define EVENT_DESC 0 |
AxedaCorp | 0:a725e8eab383 | 39 | |
AxedaCorp | 0:a725e8eab383 | 40 | #define LOC_LAT 0 |
AxedaCorp | 0:a725e8eab383 | 41 | #define LOC_LON 1 |
AxedaCorp | 0:a725e8eab383 | 42 | #define LOC_ALT 2 |
AxedaCorp | 0:a725e8eab383 | 43 | |
AxedaCorp | 0:a725e8eab383 | 44 | int terse_enable=AX_TRUE; |
AxedaCorp | 0:a725e8eab383 | 45 | |
AxedaCorp | 0:a725e8eab383 | 46 | const char *commonKW[]= {"time", "priority", "name", "description" }; |
AxedaCorp | 0:a725e8eab383 | 47 | const char *dataKW[]={ "alerts", "events", "data", "locations", "dataItems"}; |
AxedaCorp | 0:a725e8eab383 | 48 | const char *registrationKW[]= {"id", "mn", "sn", "tn", "pingRate", "key"}; |
AxedaCorp | 0:a725e8eab383 | 49 | const char *alertKW[]= {"severity","cause", "reason" }; |
AxedaCorp | 0:a725e8eab383 | 50 | const char *locationKW[]= {"latitude", "longitude", "altitude"}; |
AxedaCorp | 0:a725e8eab383 | 51 | const char *pkgStatusKW[]={"status", "error"}; |
AxedaCorp | 0:a725e8eab383 | 52 | |
AxedaCorp | 0:a725e8eab383 | 53 | const char *terse_commonKW[]= {"t", "dp", "n", "d"}; |
AxedaCorp | 0:a725e8eab383 | 54 | const char *terse_dataKW[]={ "alerts", "events", "data", "locations", "di"}; |
AxedaCorp | 0:a725e8eab383 | 55 | const char *terse_registrationKW[]= {"id", "mn", "sn", "tn", "pr", "k"}; |
AxedaCorp | 0:a725e8eab383 | 56 | const char *terse_alertKW[]= {"sv", "cn", "cr" }; |
AxedaCorp | 0:a725e8eab383 | 57 | const char *terse_locationKW[]= {"lat", "lon", "alt"}; |
AxedaCorp | 0:a725e8eab383 | 58 | const char *terse_pkgStatusKW[]={"st", "err"}; |
AxedaCorp | 0:a725e8eab383 | 59 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 60 | /*dataSet2JSON() */ |
AxedaCorp | 0:a725e8eab383 | 61 | /* */ |
AxedaCorp | 0:a725e8eab383 | 62 | /*Takes an array of ax_dataItem structs and creates JSON to represent them in the AMMP-json */ |
AxedaCorp | 0:a725e8eab383 | 63 | /*protocol. The return type will be a CJSON pointer which can then be rendered into an actual */ |
AxedaCorp | 0:a725e8eab383 | 64 | /*JSON string. The JSON pointer will need to be free'd when you're done. If there are zero data */ |
AxedaCorp | 0:a725e8eab383 | 65 | /*items in the array then this method will return an empty JSON array pointer. */ |
AxedaCorp | 0:a725e8eab383 | 66 | /* */ |
AxedaCorp | 0:a725e8eab383 | 67 | /*NOTE: If any array spots are left empty they will only be ignored if theyre are NULL. Junk */ |
AxedaCorp | 0:a725e8eab383 | 68 | /* values from left over data will cause seg faults. NULL your empty pointers!!! */ |
AxedaCorp | 0:a725e8eab383 | 69 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 70 | //takes data item set, not longer just data item structs. |
AxedaCorp | 0:a725e8eab383 | 71 | cJSON *dataSet2JSON(ax_dataSet *di[], int len, int terse_on) |
AxedaCorp | 0:a725e8eab383 | 72 | { |
AxedaCorp | 0:a725e8eab383 | 73 | cJSON *root, *child_obj, *dis=NULL; |
AxedaCorp | 0:a725e8eab383 | 74 | int ctr=0; |
AxedaCorp | 0:a725e8eab383 | 75 | |
AxedaCorp | 0:a725e8eab383 | 76 | ax_dataSet *curr=NULL; |
AxedaCorp | 0:a725e8eab383 | 77 | ax_dataNode *currNode; |
AxedaCorp | 0:a725e8eab383 | 78 | root=cJSON_CreateArray(); |
AxedaCorp | 0:a725e8eab383 | 79 | |
AxedaCorp | 0:a725e8eab383 | 80 | for(ctr=0; ctr<len; ctr++) |
AxedaCorp | 0:a725e8eab383 | 81 | { |
AxedaCorp | 0:a725e8eab383 | 82 | curr=di[ctr]; |
AxedaCorp | 0:a725e8eab383 | 83 | if((curr!=NULL)&&(curr->dataNode_ct>0)) //If this set doesn't have any data in it, don't include it. |
AxedaCorp | 0:a725e8eab383 | 84 | { |
AxedaCorp | 0:a725e8eab383 | 85 | cJSON_AddItemToArray(root, child_obj=cJSON_CreateObject()); |
AxedaCorp | 0:a725e8eab383 | 86 | if(terse_on==AX_TRUE) { |
AxedaCorp | 0:a725e8eab383 | 87 | cJSON_AddItemToObject(child_obj, terse_dataKW[DATA_ITEMS], dis=cJSON_CreateObject()); } |
AxedaCorp | 0:a725e8eab383 | 88 | else { |
AxedaCorp | 0:a725e8eab383 | 89 | cJSON_AddItemToObject(child_obj, dataKW[DATA_ITEMS], dis=cJSON_CreateObject()); |
AxedaCorp | 0:a725e8eab383 | 90 | } |
AxedaCorp | 0:a725e8eab383 | 91 | if((curr->acquisitionTime>=0)&&(curr->acquisitionTime)){ |
AxedaCorp | 0:a725e8eab383 | 92 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 93 | cJSON_AddNumberToObject(child_obj, terse_commonKW[COM_TIME], curr->acquisitionTime); |
AxedaCorp | 0:a725e8eab383 | 94 | } |
AxedaCorp | 0:a725e8eab383 | 95 | else { |
AxedaCorp | 0:a725e8eab383 | 96 | cJSON_AddNumberToObject(child_obj, commonKW[COM_TIME], curr->acquisitionTime); |
AxedaCorp | 0:a725e8eab383 | 97 | } |
AxedaCorp | 0:a725e8eab383 | 98 | } |
AxedaCorp | 0:a725e8eab383 | 99 | if((curr->priority>=1)&&(curr->priority<=100)) { |
AxedaCorp | 0:a725e8eab383 | 100 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 101 | cJSON_AddNumberToObject(child_obj, terse_commonKW[COM_PRIORITY], curr->priority); |
AxedaCorp | 0:a725e8eab383 | 102 | } |
AxedaCorp | 0:a725e8eab383 | 103 | else { |
AxedaCorp | 0:a725e8eab383 | 104 | cJSON_AddNumberToObject(child_obj, commonKW[COM_PRIORITY], curr->priority); |
AxedaCorp | 0:a725e8eab383 | 105 | } |
AxedaCorp | 0:a725e8eab383 | 106 | } |
AxedaCorp | 0:a725e8eab383 | 107 | currNode=curr->data_first; |
AxedaCorp | 0:a725e8eab383 | 108 | while(currNode!=NULL) |
AxedaCorp | 0:a725e8eab383 | 109 | { |
AxedaCorp | 0:a725e8eab383 | 110 | if(currNode->type==AX_DIGITAL) { |
AxedaCorp | 0:a725e8eab383 | 111 | if(currNode->dValue==1) { |
AxedaCorp | 0:a725e8eab383 | 112 | cJSON_AddTrueToObject(dis, currNode->name); |
AxedaCorp | 0:a725e8eab383 | 113 | } |
AxedaCorp | 0:a725e8eab383 | 114 | else { |
AxedaCorp | 0:a725e8eab383 | 115 | cJSON_AddFalseToObject(dis, currNode->name); |
AxedaCorp | 0:a725e8eab383 | 116 | } |
AxedaCorp | 0:a725e8eab383 | 117 | } |
AxedaCorp | 0:a725e8eab383 | 118 | else if(currNode->type==AX_ANALOG) { |
AxedaCorp | 0:a725e8eab383 | 119 | cJSON_AddNumberToObject(dis, currNode->name, currNode->dValue); |
AxedaCorp | 0:a725e8eab383 | 120 | } |
AxedaCorp | 0:a725e8eab383 | 121 | else { |
AxedaCorp | 0:a725e8eab383 | 122 | cJSON_AddStringToObject(dis, currNode->name, currNode->sValue); |
AxedaCorp | 0:a725e8eab383 | 123 | } |
AxedaCorp | 0:a725e8eab383 | 124 | //advance to the next data item |
AxedaCorp | 0:a725e8eab383 | 125 | currNode=currNode->next; |
AxedaCorp | 0:a725e8eab383 | 126 | } |
AxedaCorp | 0:a725e8eab383 | 127 | } |
AxedaCorp | 0:a725e8eab383 | 128 | } |
AxedaCorp | 0:a725e8eab383 | 129 | return root; |
AxedaCorp | 0:a725e8eab383 | 130 | } |
AxedaCorp | 0:a725e8eab383 | 131 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 132 | /* eventsToJSON() */ |
AxedaCorp | 0:a725e8eab383 | 133 | /* */ |
AxedaCorp | 0:a725e8eab383 | 134 | /*Takes an array of ax_event struct pointers and creates JSON to represent them in the AMMP-json */ |
AxedaCorp | 0:a725e8eab383 | 135 | /*protocol. The return type will be a CJSON pointer which can then be rendered into an actual */ |
AxedaCorp | 0:a725e8eab383 | 136 | /*JSON string. The JSON pointer will need to be free'd when you're done. If there are zero data */ |
AxedaCorp | 0:a725e8eab383 | 137 | /*items in the array then this method will return an empty JSON array pointer. */ |
AxedaCorp | 0:a725e8eab383 | 138 | /* */ |
AxedaCorp | 0:a725e8eab383 | 139 | /*NOTE: If any array spots are left empty they will only be ignored if theyre are NULL. Junk */ |
AxedaCorp | 0:a725e8eab383 | 140 | /* values from left over data will cause seg faults. NULL your empty pointers!!! */ |
AxedaCorp | 0:a725e8eab383 | 141 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 142 | cJSON *eventsToJSON(ax_event *events[], int len, int terse_on) { |
AxedaCorp | 0:a725e8eab383 | 143 | cJSON *root, *dis; |
AxedaCorp | 0:a725e8eab383 | 144 | ax_event *curr; |
AxedaCorp | 0:a725e8eab383 | 145 | int ctr=0; |
AxedaCorp | 0:a725e8eab383 | 146 | root=cJSON_CreateArray(); |
AxedaCorp | 0:a725e8eab383 | 147 | curr=events[0]; |
AxedaCorp | 0:a725e8eab383 | 148 | for(ctr=0; ctr<len; ctr++) |
AxedaCorp | 0:a725e8eab383 | 149 | { |
AxedaCorp | 0:a725e8eab383 | 150 | curr=events[ctr]; |
AxedaCorp | 0:a725e8eab383 | 151 | if(curr!=NULL) |
AxedaCorp | 0:a725e8eab383 | 152 | { |
AxedaCorp | 0:a725e8eab383 | 153 | cJSON_AddItemToArray(root, dis=cJSON_CreateObject()); |
AxedaCorp | 0:a725e8eab383 | 154 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 155 | cJSON_AddStringToObject(dis, terse_commonKW[COM_NAME], curr->name); |
AxedaCorp | 0:a725e8eab383 | 156 | cJSON_AddStringToObject(dis, terse_commonKW[COM_DESCRIPTION], curr->description); |
AxedaCorp | 0:a725e8eab383 | 157 | } |
AxedaCorp | 0:a725e8eab383 | 158 | else { |
AxedaCorp | 0:a725e8eab383 | 159 | cJSON_AddStringToObject(dis, commonKW[COM_NAME], curr->name); |
AxedaCorp | 0:a725e8eab383 | 160 | cJSON_AddStringToObject(dis, commonKW[COM_DESCRIPTION], curr->description); |
AxedaCorp | 0:a725e8eab383 | 161 | } |
AxedaCorp | 0:a725e8eab383 | 162 | |
AxedaCorp | 0:a725e8eab383 | 163 | if((curr->dateAcquired>=0)&&(curr->dateAcquired)){ |
AxedaCorp | 0:a725e8eab383 | 164 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 165 | cJSON_AddNumberToObject(dis, terse_commonKW[COM_TIME], curr->dateAcquired); |
AxedaCorp | 0:a725e8eab383 | 166 | } |
AxedaCorp | 0:a725e8eab383 | 167 | else { |
AxedaCorp | 0:a725e8eab383 | 168 | cJSON_AddNumberToObject(dis, commonKW[COM_TIME], curr->dateAcquired); |
AxedaCorp | 0:a725e8eab383 | 169 | } |
AxedaCorp | 0:a725e8eab383 | 170 | } |
AxedaCorp | 0:a725e8eab383 | 171 | if((curr->priority>=1)&&(curr->priority<=100)&&(curr->priority)) { |
AxedaCorp | 0:a725e8eab383 | 172 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 173 | cJSON_AddNumberToObject(dis, terse_commonKW[COM_PRIORITY], curr->priority); |
AxedaCorp | 0:a725e8eab383 | 174 | } |
AxedaCorp | 0:a725e8eab383 | 175 | else { |
AxedaCorp | 0:a725e8eab383 | 176 | cJSON_AddNumberToObject(dis, commonKW[COM_PRIORITY], curr->priority); |
AxedaCorp | 0:a725e8eab383 | 177 | } |
AxedaCorp | 0:a725e8eab383 | 178 | } |
AxedaCorp | 0:a725e8eab383 | 179 | } |
AxedaCorp | 0:a725e8eab383 | 180 | } |
AxedaCorp | 0:a725e8eab383 | 181 | |
AxedaCorp | 0:a725e8eab383 | 182 | return root; |
AxedaCorp | 0:a725e8eab383 | 183 | } |
AxedaCorp | 0:a725e8eab383 | 184 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 185 | /* locationsToJSON() */ |
AxedaCorp | 0:a725e8eab383 | 186 | /* */ |
AxedaCorp | 0:a725e8eab383 | 187 | /*Takes an array of ax_location struct pointers and creates JSON to represent them in the AMMP */ |
AxedaCorp | 0:a725e8eab383 | 188 | /*protocol. The return type will be a CJSON pointer which can then be rendered into an actual */ |
AxedaCorp | 0:a725e8eab383 | 189 | /*JSON string. The JSON pointer will need to be free'd when you're done. If there are zero data */ |
AxedaCorp | 0:a725e8eab383 | 190 | /*items in the array then this method will return an empty JSON array pointer. */ |
AxedaCorp | 0:a725e8eab383 | 191 | /* */ |
AxedaCorp | 0:a725e8eab383 | 192 | /*NOTE: If any array spots are left empty they will only be ignored if theyre are NULL. Junk */ |
AxedaCorp | 0:a725e8eab383 | 193 | /* values from left over data will cause seg faults. NULL your empty pointers!!! */ |
AxedaCorp | 0:a725e8eab383 | 194 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 195 | cJSON *locationsToJSON(ax_location *locations[], int len, int terse_on) { |
AxedaCorp | 0:a725e8eab383 | 196 | cJSON *root, *dis; |
AxedaCorp | 0:a725e8eab383 | 197 | ax_location *curr; |
AxedaCorp | 0:a725e8eab383 | 198 | int ctr=0; |
AxedaCorp | 0:a725e8eab383 | 199 | root=cJSON_CreateArray(); |
AxedaCorp | 0:a725e8eab383 | 200 | for(ctr=0; ctr<len; ctr++) |
AxedaCorp | 0:a725e8eab383 | 201 | { |
AxedaCorp | 0:a725e8eab383 | 202 | curr=locations[ctr]; |
AxedaCorp | 0:a725e8eab383 | 203 | if(curr) |
AxedaCorp | 0:a725e8eab383 | 204 | { |
AxedaCorp | 0:a725e8eab383 | 205 | cJSON_AddItemToArray(root, dis=cJSON_CreateObject()); |
AxedaCorp | 0:a725e8eab383 | 206 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 207 | cJSON_AddNumberToObject(dis, terse_locationKW[LOC_LAT], curr->latitude); |
AxedaCorp | 0:a725e8eab383 | 208 | cJSON_AddNumberToObject(dis, terse_locationKW[LOC_LON], curr->longitude); |
AxedaCorp | 0:a725e8eab383 | 209 | cJSON_AddNumberToObject(dis, terse_locationKW[LOC_ALT], curr->altitude); |
AxedaCorp | 0:a725e8eab383 | 210 | } |
AxedaCorp | 0:a725e8eab383 | 211 | else { |
AxedaCorp | 0:a725e8eab383 | 212 | cJSON_AddNumberToObject(dis, locationKW[LOC_LAT], curr->latitude); |
AxedaCorp | 0:a725e8eab383 | 213 | cJSON_AddNumberToObject(dis, locationKW[LOC_LON], curr->longitude); |
AxedaCorp | 0:a725e8eab383 | 214 | cJSON_AddNumberToObject(dis, locationKW[LOC_ALT], curr->altitude); |
AxedaCorp | 0:a725e8eab383 | 215 | } |
AxedaCorp | 0:a725e8eab383 | 216 | if((curr->dateAcquired)&&(curr->dateAcquired>=0)){ |
AxedaCorp | 0:a725e8eab383 | 217 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 218 | cJSON_AddNumberToObject(dis, terse_commonKW[COM_TIME], curr->dateAcquired); |
AxedaCorp | 0:a725e8eab383 | 219 | } |
AxedaCorp | 0:a725e8eab383 | 220 | else { |
AxedaCorp | 0:a725e8eab383 | 221 | cJSON_AddNumberToObject(dis, commonKW[COM_TIME], curr->dateAcquired); |
AxedaCorp | 0:a725e8eab383 | 222 | } |
AxedaCorp | 0:a725e8eab383 | 223 | } |
AxedaCorp | 0:a725e8eab383 | 224 | if((curr->priority>=1)&&(curr->priority<=100)&&(curr->priority)) { |
AxedaCorp | 0:a725e8eab383 | 225 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 226 | cJSON_AddNumberToObject(dis, terse_commonKW[COM_PRIORITY], curr->priority); |
AxedaCorp | 0:a725e8eab383 | 227 | } |
AxedaCorp | 0:a725e8eab383 | 228 | else { |
AxedaCorp | 0:a725e8eab383 | 229 | cJSON_AddNumberToObject(dis, commonKW[COM_PRIORITY], curr->priority); |
AxedaCorp | 0:a725e8eab383 | 230 | } |
AxedaCorp | 0:a725e8eab383 | 231 | } |
AxedaCorp | 0:a725e8eab383 | 232 | } |
AxedaCorp | 0:a725e8eab383 | 233 | } |
AxedaCorp | 0:a725e8eab383 | 234 | |
AxedaCorp | 0:a725e8eab383 | 235 | return root; |
AxedaCorp | 0:a725e8eab383 | 236 | } |
AxedaCorp | 0:a725e8eab383 | 237 | |
AxedaCorp | 0:a725e8eab383 | 238 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 239 | /* alarmsToJSON() */ |
AxedaCorp | 0:a725e8eab383 | 240 | /* */ |
AxedaCorp | 0:a725e8eab383 | 241 | /*Takes an array of ax_alarm struct pointers and creates JSON to represent them in the AMMP-json */ |
AxedaCorp | 0:a725e8eab383 | 242 | /*protocol. The return type will be a CJSON pointer which can then be rendered into an actual */ |
AxedaCorp | 0:a725e8eab383 | 243 | /*JSON string. The JSON pointer will need to be free'd when you're done. If there are zero data */ |
AxedaCorp | 0:a725e8eab383 | 244 | /*items in the array then this method will return an empty JSON array pointer. */ |
AxedaCorp | 0:a725e8eab383 | 245 | /* */ |
AxedaCorp | 0:a725e8eab383 | 246 | /*NOTE: If any array spots are left empty they will only be ignored if theyre are NULL. Junk */ |
AxedaCorp | 0:a725e8eab383 | 247 | /* values from left over data will cause seg faults. NULL your empty pointers!!! */ |
AxedaCorp | 0:a725e8eab383 | 248 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 249 | cJSON *AlarmsToJSON(ax_alarm *alarms[], int len, int terse_on) |
AxedaCorp | 0:a725e8eab383 | 250 | { |
AxedaCorp | 0:a725e8eab383 | 251 | cJSON *root, *dis; |
AxedaCorp | 0:a725e8eab383 | 252 | int ctr=0; |
AxedaCorp | 0:a725e8eab383 | 253 | ax_alarm *curr; |
AxedaCorp | 0:a725e8eab383 | 254 | root=cJSON_CreateArray(); |
AxedaCorp | 0:a725e8eab383 | 255 | curr=alarms[0]; |
AxedaCorp | 0:a725e8eab383 | 256 | for(ctr=0; ctr<len; ctr++) { |
AxedaCorp | 0:a725e8eab383 | 257 | curr=alarms[ctr]; |
AxedaCorp | 0:a725e8eab383 | 258 | if(curr!=NULL) |
AxedaCorp | 0:a725e8eab383 | 259 | { |
AxedaCorp | 0:a725e8eab383 | 260 | cJSON_AddItemToArray(root, dis=cJSON_CreateObject()); |
AxedaCorp | 0:a725e8eab383 | 261 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 262 | cJSON_AddStringToObject(dis, terse_commonKW[COM_NAME], curr->alarmName); |
AxedaCorp | 0:a725e8eab383 | 263 | cJSON_AddStringToObject(dis, terse_commonKW[COM_DESCRIPTION], curr->alarmDescription); |
AxedaCorp | 0:a725e8eab383 | 264 | cJSON_AddNumberToObject(dis, terse_alertKW[ALERT_SEV], curr->alarmSeverity); |
AxedaCorp | 0:a725e8eab383 | 265 | cJSON_AddStringToObject(dis, terse_alertKW[ALERT_CAUSE], curr->alarmCause); |
AxedaCorp | 0:a725e8eab383 | 266 | cJSON_AddStringToObject(dis, terse_alertKW[ALERT_REASON], curr->alarmReason); |
AxedaCorp | 0:a725e8eab383 | 267 | } |
AxedaCorp | 0:a725e8eab383 | 268 | else { |
AxedaCorp | 0:a725e8eab383 | 269 | cJSON_AddStringToObject(dis, commonKW[COM_NAME], curr->alarmName); |
AxedaCorp | 0:a725e8eab383 | 270 | cJSON_AddStringToObject(dis, commonKW[COM_DESCRIPTION], curr->alarmDescription); |
AxedaCorp | 0:a725e8eab383 | 271 | cJSON_AddNumberToObject(dis, alertKW[ALERT_SEV], curr->alarmSeverity); |
AxedaCorp | 0:a725e8eab383 | 272 | cJSON_AddStringToObject(dis, alertKW[ALERT_CAUSE], curr->alarmCause); |
AxedaCorp | 0:a725e8eab383 | 273 | cJSON_AddStringToObject(dis, alertKW[ALERT_REASON], curr->alarmReason); |
AxedaCorp | 0:a725e8eab383 | 274 | } |
AxedaCorp | 0:a725e8eab383 | 275 | |
AxedaCorp | 0:a725e8eab383 | 276 | if((curr->dateAcquired)&&(curr->dateAcquired>=0)){ |
AxedaCorp | 0:a725e8eab383 | 277 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 278 | cJSON_AddNumberToObject(dis, terse_commonKW[COM_TIME], curr->dateAcquired); |
AxedaCorp | 0:a725e8eab383 | 279 | } |
AxedaCorp | 0:a725e8eab383 | 280 | else { |
AxedaCorp | 0:a725e8eab383 | 281 | cJSON_AddNumberToObject(dis, commonKW[COM_TIME], curr->dateAcquired); |
AxedaCorp | 0:a725e8eab383 | 282 | } |
AxedaCorp | 0:a725e8eab383 | 283 | } |
AxedaCorp | 0:a725e8eab383 | 284 | if((curr->priority>=1)&&(curr->priority<=100)&&(curr->priority)) { |
AxedaCorp | 0:a725e8eab383 | 285 | if(terse_on==AX_TRUE){ |
AxedaCorp | 0:a725e8eab383 | 286 | cJSON_AddNumberToObject(dis, terse_commonKW[COM_PRIORITY], curr->priority); |
AxedaCorp | 0:a725e8eab383 | 287 | } |
AxedaCorp | 0:a725e8eab383 | 288 | else { |
AxedaCorp | 0:a725e8eab383 | 289 | cJSON_AddNumberToObject(dis, commonKW[COM_PRIORITY], curr->priority); |
AxedaCorp | 0:a725e8eab383 | 290 | } |
AxedaCorp | 0:a725e8eab383 | 291 | } |
AxedaCorp | 0:a725e8eab383 | 292 | } |
AxedaCorp | 0:a725e8eab383 | 293 | } |
AxedaCorp | 0:a725e8eab383 | 294 | |
AxedaCorp | 0:a725e8eab383 | 295 | return root; |
AxedaCorp | 0:a725e8eab383 | 296 | } |
AxedaCorp | 0:a725e8eab383 | 297 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 298 | /* getRegistrationJSON() */ |
AxedaCorp | 0:a725e8eab383 | 299 | /* */ |
AxedaCorp | 0:a725e8eab383 | 300 | /*Takes a device and ping rate and will return a cJSON structure that represents a registration */ |
AxedaCorp | 0:a725e8eab383 | 301 | /*message in the AMMP protocol. */ |
AxedaCorp | 0:a725e8eab383 | 302 | /* */ |
AxedaCorp | 0:a725e8eab383 | 303 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 304 | cJSON *getRegistrationJSON(ax_deviceID *device, int pingRate) |
AxedaCorp | 0:a725e8eab383 | 305 | { |
AxedaCorp | 0:a725e8eab383 | 306 | cJSON *root, *child; |
AxedaCorp | 0:a725e8eab383 | 307 | |
AxedaCorp | 0:a725e8eab383 | 308 | root=cJSON_CreateObject(); |
AxedaCorp | 0:a725e8eab383 | 309 | |
AxedaCorp | 0:a725e8eab383 | 310 | cJSON_AddItemToObject(root, registrationKW[REG_ID], child=cJSON_CreateObject()); |
AxedaCorp | 0:a725e8eab383 | 311 | if(device->model) { |
AxedaCorp | 0:a725e8eab383 | 312 | cJSON_AddStringToObject(child, registrationKW[REG_MODEL_NUMBER], device->model); |
AxedaCorp | 0:a725e8eab383 | 313 | } |
AxedaCorp | 0:a725e8eab383 | 314 | if(device->serial) { |
AxedaCorp | 0:a725e8eab383 | 315 | cJSON_AddStringToObject(child, registrationKW[REG_SERIAL_NUMBER], device->serial); |
AxedaCorp | 0:a725e8eab383 | 316 | } |
AxedaCorp | 0:a725e8eab383 | 317 | if(strlen(device->tenant)>=1) |
AxedaCorp | 0:a725e8eab383 | 318 | { |
AxedaCorp | 0:a725e8eab383 | 319 | cJSON_AddStringToObject(child, registrationKW[REG_TENANT], device->tenant); |
AxedaCorp | 0:a725e8eab383 | 320 | } |
AxedaCorp | 0:a725e8eab383 | 321 | cJSON_AddNumberToObject(root, registrationKW[REG_PING], pingRate); |
AxedaCorp | 0:a725e8eab383 | 322 | |
AxedaCorp | 0:a725e8eab383 | 323 | |
AxedaCorp | 0:a725e8eab383 | 324 | return root; |
AxedaCorp | 0:a725e8eab383 | 325 | } |
AxedaCorp | 0:a725e8eab383 | 326 | |
AxedaCorp | 0:a725e8eab383 | 327 | //TODO: Fill out this description |
AxedaCorp | 0:a725e8eab383 | 328 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 329 | /*getPKGStatusJSON() */ |
AxedaCorp | 0:a725e8eab383 | 330 | /* */ |
AxedaCorp | 0:a725e8eab383 | 331 | /* */ |
AxedaCorp | 0:a725e8eab383 | 332 | /* */ |
AxedaCorp | 0:a725e8eab383 | 333 | /*************************************************************************************************/ |
AxedaCorp | 0:a725e8eab383 | 334 | cJSON *getPKGStatusJSON(int status, char *error, int priority, int time, int terse_on){ |
AxedaCorp | 0:a725e8eab383 | 335 | cJSON *root; |
AxedaCorp | 0:a725e8eab383 | 336 | root=cJSON_CreateObject(); |
AxedaCorp | 0:a725e8eab383 | 337 | //STATUS |
AxedaCorp | 0:a725e8eab383 | 338 | if(terse_on==AX_TRUE) { |
AxedaCorp | 0:a725e8eab383 | 339 | cJSON_AddNumberToObject(root, pkgStatusKW[PKG_STATUS], status); |
AxedaCorp | 0:a725e8eab383 | 340 | } |
AxedaCorp | 0:a725e8eab383 | 341 | else { |
AxedaCorp | 0:a725e8eab383 | 342 | cJSON_AddNumberToObject(root, pkgStatusKW[PKG_STATUS], status); |
AxedaCorp | 0:a725e8eab383 | 343 | } |
AxedaCorp | 0:a725e8eab383 | 344 | //TIME |
AxedaCorp | 0:a725e8eab383 | 345 | if(time>=0) { |
AxedaCorp | 0:a725e8eab383 | 346 | if(terse_on==AX_TRUE) { |
AxedaCorp | 0:a725e8eab383 | 347 | cJSON_AddNumberToObject(root, terse_commonKW[COM_TIME], time); |
AxedaCorp | 0:a725e8eab383 | 348 | } |
AxedaCorp | 0:a725e8eab383 | 349 | else { |
AxedaCorp | 0:a725e8eab383 | 350 | cJSON_AddNumberToObject(root, commonKW[COM_TIME], time); |
AxedaCorp | 0:a725e8eab383 | 351 | } |
AxedaCorp | 0:a725e8eab383 | 352 | } |
AxedaCorp | 0:a725e8eab383 | 353 | //PRIORITY |
AxedaCorp | 0:a725e8eab383 | 354 | if((priority>0)&&(priority<=100)) { |
AxedaCorp | 0:a725e8eab383 | 355 | if(terse_on==AX_TRUE) { |
AxedaCorp | 0:a725e8eab383 | 356 | cJSON_AddNumberToObject(root, terse_commonKW[COM_PRIORITY], priority); |
AxedaCorp | 0:a725e8eab383 | 357 | } |
AxedaCorp | 0:a725e8eab383 | 358 | else { |
AxedaCorp | 0:a725e8eab383 | 359 | cJSON_AddNumberToObject(root, commonKW[COM_PRIORITY], priority); |
AxedaCorp | 0:a725e8eab383 | 360 | } |
AxedaCorp | 0:a725e8eab383 | 361 | } |
AxedaCorp | 0:a725e8eab383 | 362 | //ERROR |
AxedaCorp | 0:a725e8eab383 | 363 | if(error!=NULL) { |
AxedaCorp | 0:a725e8eab383 | 364 | if(terse_on==AX_TRUE) { |
AxedaCorp | 0:a725e8eab383 | 365 | cJSON_AddStringToObject(root, terse_pkgStatusKW[PKG_ERROR], error); |
AxedaCorp | 0:a725e8eab383 | 366 | } |
AxedaCorp | 0:a725e8eab383 | 367 | else { |
AxedaCorp | 0:a725e8eab383 | 368 | cJSON_AddStringToObject(root, pkgStatusKW[PKG_ERROR], error); |
AxedaCorp | 0:a725e8eab383 | 369 | } |
AxedaCorp | 0:a725e8eab383 | 370 | } |
AxedaCorp | 0:a725e8eab383 | 371 | return root; |
AxedaCorp | 0:a725e8eab383 | 372 | } |
AxedaCorp | 0:a725e8eab383 | 373 | |
AxedaCorp | 0:a725e8eab383 | 374 |