|
| 1 | + |
| 2 | +#include "MQTTClient.h" |
| 3 | + |
| 4 | +/* Use the file storage command */ |
| 5 | +#define E4_STORE_FILE |
| 6 | +#include "e4/e4.h" |
| 7 | +#include "e4/strlcpy.h" |
| 8 | +#include "e4/util.h" |
| 9 | + |
| 10 | +/* local header includes */ |
| 11 | +#include "e4cli.h" |
| 12 | + |
| 13 | + |
| 14 | +void client_setid(e4client *client, const char *arg) |
| 15 | +{ |
| 16 | + uint8_t id[E4_ID_LEN]; |
| 17 | + size_t arglen = strlen(arg); |
| 18 | + int result = 0; |
| 19 | + |
| 20 | + memset(id, 0, sizeof id); |
| 21 | + |
| 22 | + if (arglen == 0 || arglen != E4_ID_LEN * 2) |
| 23 | + { |
| 24 | + printf("Expected client ID of length %d, not received.\n", E4_ID_LEN * 2); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + result = e4c_hex_decode((char*)id, sizeof(id), (char*)arg, arglen); |
| 29 | + if (result == 0) |
| 30 | + { |
| 31 | + printf("Unable to decode ID: invalid data.\n"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + client->store.e4c_set_id(client->store, id); |
| 36 | + return; |
| 37 | +} |
| 38 | + |
| 39 | +void client_genkey(const char *arg) |
| 40 | +{ |
| 41 | + |
| 42 | + // do not generate but ask the C2 to do it? |
| 43 | + // can clients do this? |
| 44 | + // TODO: resolve above quesitons |
| 45 | + |
| 46 | + printf("ERROR: not implemented in this client.\n"); |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +void client_setalias(e4client *client, const char *arg) |
| 51 | +{ |
| 52 | + |
| 53 | + char id[E4_ID_LEN] = { 0 }; |
| 54 | + |
| 55 | + e4c_derive_clientid(id, sizeof id, arg, arglen); |
| 56 | + memcpy(client->store.id, id, E4_ID_LEN); |
| 57 | + e4c_sync(client->store); |
| 58 | +} |
| 59 | + |
| 60 | +// sets the client key to a given value when supplied on the |
| 61 | +// command line. |
| 62 | +void client_setkey(e4client *client, const char *arg) |
| 63 | +{ |
| 64 | + char keybuffer[E4_KEY_LEN]; |
| 65 | + size_t keylen = strlen(arg); |
| 66 | + if (keylen != E4_KEY_LEN * 2) |
| 67 | + { |
| 68 | + printf("ERROR: key length must be %d characters, encoded in hex.\n", E4_KEY_LEN * 2); |
| 69 | + return; |
| 70 | + } |
| 71 | + e4c_hex_decode(keybuffer, E4_KEY_LEN, arg, keylen); |
| 72 | + e4c_set_idkey(client->store, keybuffer); |
| 73 | +} |
| 74 | + |
| 75 | +void client_settopickey(const char *arg) |
| 76 | +{ |
| 77 | + char keybuffer[E4_KEY_LEN]; |
| 78 | + char *topic = arg; |
| 79 | + char *key = arg; |
| 80 | + |
| 81 | + key = strchr(arg, ' '); |
| 82 | + if (key == NULL || key == arg) |
| 83 | + { |
| 84 | + printf("ERROR: must specify a topic and key in the format:\n"); |
| 85 | + printf(" <topicname> <key>\n"); |
| 86 | + printf(" key must be %d hexadecimal characters.", 2 * E4_KEY_LEN); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // tokenize the strings. |
| 91 | + key[0] = '\0'; |
| 92 | + key += 1; |
| 93 | + |
| 94 | + size_t keylen = strlen(key); |
| 95 | + if (keylen != E4_KEY_LEN * 2) |
| 96 | + { |
| 97 | + printf("ERROR: key length must be %d characters, encoded in hex.\n", E4_KEY_LEN * 2); |
| 98 | + return; |
| 99 | + } |
| 100 | + e4c_hex_decode(keybuffer, E4_KEY_LEN, key, keylen); |
| 101 | + e4c_set_topic_key(client->store, keybuffer); |
| 102 | +} |
| 103 | + |
| 104 | +void client_setpwd(const char *arg) |
| 105 | +{ |
| 106 | + printf("ERROR: The C client does not yet implement password-derivation.\n"); |
| 107 | + printf(" Please use the setkey command.\n"); |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +void client_subscribe(const char *arg) |
| 112 | +{ |
| 113 | + |
| 114 | + size_t arglen = strlen(arg); |
| 115 | + if (arglen == 0) i |
| 116 | + { |
| 117 | + printf("client_subscribe: Invalid topic"); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + MQTTClient_subscribe(handle, arg, 0); |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +void client_unsubscribe(const char *arg) |
| 126 | +{ |
| 127 | + size_t arglen = strlen(arg); |
| 128 | + if (arglen == 0) |
| 129 | + { |
| 130 | + printf("client_subscribe: Invalid topic"); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + MQTTClient_unsubscribe(handle, arg); |
| 135 | +} |
| 136 | + |
| 137 | +void client_changetopic(const char *arg) {} |
| 138 | + |
| 139 | +void client_e4msg(const char *arg) {} |
| 140 | + |
| 141 | +void client_clearmsg(const char *arg) {} |
| 142 | + |
| 143 | +void client_list(const char *arg) {} |
| 144 | + |
| 145 | +void client_zero(const char *arg) {} |
0 commit comments