Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
util/SmartRestConf.cpp@117:5de54f09f754, 2015-05-20 (annotated)
- Committer:
- xinlei
- Date:
- Wed May 20 09:55:49 2015 +0000
- Revision:
- 117:5de54f09f754
- Parent:
- 113:3872569be2af
- Child:
- 130:dc9e37d4bc05
- Child:
- 131:ca312ec4bd0f
device bootstrap revamp
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| xinlei | 93:0acd11870c6a | 1 | #include <string.h> |
| xinlei | 94:61d44636f020 | 2 | #include <stdio.h> |
| xinlei | 93:0acd11870c6a | 3 | #include "b64.h" |
| xinlei | 99:e369fc75c000 | 4 | #include "SmartRestConf.h" |
| xinlei | 93:0acd11870c6a | 5 | |
| xinlei | 113:3872569be2af | 6 | char srTenant[CREDENTIAL_LENGTH] = {0}; |
| xinlei | 113:3872569be2af | 7 | char srUsername[CREDENTIAL_LENGTH] = {0}; |
| xinlei | 113:3872569be2af | 8 | char srPassword[CREDENTIAL_LENGTH] = {0}; |
| xinlei | 99:e369fc75c000 | 9 | char srAuthStr[100] = {0}; |
| xinlei | 117:5de54f09f754 | 10 | //const char *srX_ID = "com_cumulocity_MbedAgent_1.5.2"; |
| xinlei | 117:5de54f09f754 | 11 | const char *srX_ID = NULL; |
| xinlei | 99:e369fc75c000 | 12 | const char *srHost = "developer.cumulocity.com"; |
| xinlei | 101:dbcd3bc51758 | 13 | //const char *srHost = "management.m2m-devicecloud.com"; |
| xinlei | 99:e369fc75c000 | 14 | long deviceID = 0; |
| xinlei | 94:61d44636f020 | 15 | char fmtSmartRest[200] = {0}; |
| xinlei | 93:0acd11870c6a | 16 | |
| xinlei | 106:c61f0d62b625 | 17 | static void setAuthStr(const char* p1, const char* p2) |
| xinlei | 93:0acd11870c6a | 18 | { |
| xinlei | 99:e369fc75c000 | 19 | memset(srAuthStr, 0, sizeof(srAuthStr)); |
| xinlei | 93:0acd11870c6a | 20 | size_t ul = strlen(p1); |
| xinlei | 93:0acd11870c6a | 21 | size_t pl = strlen(p2); |
| xinlei | 93:0acd11870c6a | 22 | unsigned char input[3], output[5]; |
| xinlei | 93:0acd11870c6a | 23 | int inputOffset = 0; |
| xinlei | 93:0acd11870c6a | 24 | |
| xinlei | 93:0acd11870c6a | 25 | for (size_t i = 0; i < (ul+1+pl); i++) { |
| xinlei | 93:0acd11870c6a | 26 | if (i < ul) |
| xinlei | 93:0acd11870c6a | 27 | input[inputOffset++] = p1[i]; |
| xinlei | 93:0acd11870c6a | 28 | else if (i == ul) |
| xinlei | 93:0acd11870c6a | 29 | input[inputOffset++] = ':'; |
| xinlei | 93:0acd11870c6a | 30 | else |
| xinlei | 93:0acd11870c6a | 31 | input[inputOffset++] = p2[i-(ul+1)]; |
| xinlei | 93:0acd11870c6a | 32 | |
| xinlei | 93:0acd11870c6a | 33 | if ((inputOffset == 3) || (i == ul+pl)) { |
| xinlei | 93:0acd11870c6a | 34 | b64_encode(input, inputOffset, output, 4); |
| xinlei | 93:0acd11870c6a | 35 | output[4] = '\0'; |
| xinlei | 99:e369fc75c000 | 36 | strcat(srAuthStr, (char*)output); |
| xinlei | 93:0acd11870c6a | 37 | inputOffset = 0; |
| xinlei | 93:0acd11870c6a | 38 | } |
| xinlei | 93:0acd11870c6a | 39 | } |
| xinlei | 93:0acd11870c6a | 40 | } |
| xinlei | 93:0acd11870c6a | 41 | |
| xinlei | 113:3872569be2af | 42 | static void setTenant(const char *tenant) |
| xinlei | 93:0acd11870c6a | 43 | { |
| xinlei | 113:3872569be2af | 44 | snprintf(srTenant, sizeof(srTenant), "%s", tenant); |
| xinlei | 93:0acd11870c6a | 45 | } |
| xinlei | 93:0acd11870c6a | 46 | |
| xinlei | 113:3872569be2af | 47 | static void setUsername(const char *username) |
| xinlei | 93:0acd11870c6a | 48 | { |
| xinlei | 113:3872569be2af | 49 | snprintf(srUsername, sizeof(srUsername), "%s", username); |
| xinlei | 113:3872569be2af | 50 | } |
| xinlei | 113:3872569be2af | 51 | |
| xinlei | 113:3872569be2af | 52 | static void setPassword(const char *password) |
| xinlei | 113:3872569be2af | 53 | { |
| xinlei | 113:3872569be2af | 54 | snprintf(srPassword, sizeof(srPassword), "%s", password); |
| xinlei | 94:61d44636f020 | 55 | } |
| xinlei | 94:61d44636f020 | 56 | |
| xinlei | 106:c61f0d62b625 | 57 | static void setSmartRestFmt() |
| xinlei | 94:61d44636f020 | 58 | { |
| xinlei | 117:5de54f09f754 | 59 | if (srX_ID) { |
| xinlei | 117:5de54f09f754 | 60 | const char fmt[] = "POST %%s HTTP/1.0\r\nHost: %s\r\nAuthorization: Basic %s\r\nX-Id: %s\r\nContent-Length: %%d\r\n\r\n%%s"; |
| xinlei | 117:5de54f09f754 | 61 | snprintf(fmtSmartRest, sizeof(fmtSmartRest), fmt, srHost, srAuthStr, srX_ID); |
| xinlei | 117:5de54f09f754 | 62 | } else { |
| xinlei | 117:5de54f09f754 | 63 | const char fmt[] = "POST %%s HTTP/1.0\r\nHost: %s\r\nAuthorization: Basic %s\r\nContent-Length: %%d\r\n\r\n%%s"; |
| xinlei | 117:5de54f09f754 | 64 | snprintf(fmtSmartRest, sizeof(fmtSmartRest), fmt, srHost, srAuthStr); |
| xinlei | 117:5de54f09f754 | 65 | } |
| xinlei | 94:61d44636f020 | 66 | } |
| xinlei | 94:61d44636f020 | 67 | |
| xinlei | 99:e369fc75c000 | 68 | void setX_ID(const char* id) |
| xinlei | 94:61d44636f020 | 69 | { |
| xinlei | 94:61d44636f020 | 70 | if (id) { |
| xinlei | 99:e369fc75c000 | 71 | srX_ID = id; |
| xinlei | 94:61d44636f020 | 72 | setSmartRestFmt(); |
| xinlei | 94:61d44636f020 | 73 | } |
| xinlei | 94:61d44636f020 | 74 | } |
| xinlei | 94:61d44636f020 | 75 | |
| xinlei | 99:e369fc75c000 | 76 | void setDeviceID(long id) |
| xinlei | 99:e369fc75c000 | 77 | { |
| xinlei | 99:e369fc75c000 | 78 | deviceID = id; |
| xinlei | 99:e369fc75c000 | 79 | } |
| xinlei | 99:e369fc75c000 | 80 | |
| xinlei | 113:3872569be2af | 81 | void setAuth(const char *tenant, const char *username, const char *password) |
| xinlei | 94:61d44636f020 | 82 | { |
| xinlei | 113:3872569be2af | 83 | if (tenant) |
| xinlei | 113:3872569be2af | 84 | setTenant(tenant); |
| xinlei | 94:61d44636f020 | 85 | if (username) |
| xinlei | 94:61d44636f020 | 86 | setUsername(username); |
| xinlei | 94:61d44636f020 | 87 | if (password) |
| xinlei | 94:61d44636f020 | 88 | setPassword(password); |
| xinlei | 113:3872569be2af | 89 | if (tenant || username || password) { |
| xinlei | 113:3872569be2af | 90 | char s[CREDENTIAL_LENGTH*2]; |
| xinlei | 113:3872569be2af | 91 | snprintf(s, sizeof(s), "%s/%s", tenant, username); |
| xinlei | 113:3872569be2af | 92 | setAuthStr(s, password); |
| xinlei | 94:61d44636f020 | 93 | setSmartRestFmt(); |
| xinlei | 94:61d44636f020 | 94 | } |
| xinlei | 94:61d44636f020 | 95 | } |

Cumulocity