// called whenever another scout sends an answer back to us bool leadAnswers(NWK_DataInd_t *ind) { bool end = false; StringBuffer buf(256); if (ind->options & NWK_IND_OPT_MULTICAST) { if (hqVerboseOutput) { Serial.println(F("MULTICAST on wrong endpoint")); } return true; } if (hqVerboseOutput) { Serial.print(F("Received answer from Scout ")); Serial.print(ind->srcAddr); Serial.println(F(":")); } if (ind->data[ind->size-1] == 0) { end = true; ind->size--; } buf.appendSprintf("{\"type\":\"reply\",\"id\":%d,\"from\":%d,\"reply\":", leadAnswerID, ind->srcAddr); buf.appendJsonString(ind->data, ind->size, true); buf.appendSprintf(",\"end\":%s}\n",end ? "true" : "false"); leadSignal(buf); return true; }
// called whenever another scout sends an answer back to us bool leadAnswers(NWK_DataInd_t *ind) { bool end = false; int at; char sig[256]; if (ind->options & NWK_IND_OPT_MULTICAST) { if (hqVerboseOutput) { speol("MULTICAST on wrong endpoint"); } return true; } if (hqVerboseOutput) { speol("Received answer"); } if (ind->data[ind->size-1] == 0) { end = true; ind->size--; } sprintf(sig,"{\"type\":\"reply\",\"id\":%d,\"from\":%d,\"reply\":\"", leadAnswerID, ind->srcAddr); at = strlen(sig); memcpy(sig+at, ind->data, ind->size); sprintf(sig+at+ind->size, "\",\"end\":%s}\n",end ? "true" : "false"); leadSignal(sig); return true; }
// when we can't process a command for some internal reason void leadCommandError(int from, int id, const char *reason) { char *err; err = (char*)malloc(strlen(reason) + 128); sprintf(err,"{\"type\":\"reply\",\"from\":%d,\"id\":%d,\"err\":true,\"reply\":\"%s\"}\n", from, id, reason); leadSignal(err); free(err); }
static void leadAnnouncementSend(uint16_t group, uint16_t from, char *message) { char sig[256]; // reports are expected to be json objects if (group == 0xBEEF) { sprintf(sig, "{\"type\":\"report\",\"from\":%d,\"report\":%s}\n", from, report2json(message)); } if (group == 0) { sprintf(sig, "{\"type\":\"announce\",\"from\":%d,\"announce\":%s}\n", from, message); } leadSignal(sig); }
void leadHQConnect() { char auth[256], token[33]; if (Scout.wifi.client.connected()) { Pinoccio.getHQToken(token); token[32] = 0; sprintf(auth, "{\"type\":\"token\",\"token\":\"%s\"}\n", token); leadSignal(auth); } else { if (hqVerboseOutput) { speol("server unvailable"); } } }
void leadHQConnect() { if (Scout.wifi.client.connected()) { char token[33]; StringBuffer auth(64); token[32] = 0; Scout.getHQToken(token); auth.appendSprintf("{\"type\":\"token\",\"token\":\"%s\"}\n", token); leadSignal(auth); } else { if (hqVerboseOutput) { Serial.println(F("server unvailable")); } } }
static void leadAnnouncementSend(uint16_t group, uint16_t from, const ConstBuf& message) { StringBuffer report(100, 8); // reports are expected to be json objects if (group == 0xBEEF) { report.appendSprintf("{\"type\":\"report\",\"from\":%d,\"report\":%s}\n", from, (report2json(message)).c_str()); } else if (group == 0) { report.appendSprintf("{\"type\":\"announce\",\"from\":%d,\"announce\":", from); report.concat(message, message.length()); report += "}\n"; } else { return; } leadSignal(report); }
// process a packet from HQ void leadIncoming(const char *packet, size_t len, unsigned short *index) { char *type, *command, *buffer; buffer = (char*)malloc(len); memcpy(buffer, packet, len); uint16_t to; unsigned long id; type = j0g_str("type", buffer, index); if (hqVerboseOutput) { Serial.println(type); } if (strcmp(type, "online") == 0) { Shell.allReportHQ(); } if (strcmp(type, "command") == 0) { to = atoi(j0g_str("to", buffer, index)); id = strtoul(j0g_str("id", buffer, index), NULL, 10); command = j0g_str("command", buffer, index); if (strlen(j0g_str("to", buffer, index)) == 0 || !id || !command) { if (hqVerboseOutput) { Serial.println(F("invalid command, requires to, id, command")); Serial.print(F("to: ")); Serial.println(to); Serial.print(F("id: ")); Serial.println(id); Serial.print(F("command: ")); Serial.println(command); } free(buffer); return; } // handle internal ones first if (to == Scout.getAddress()) { setOutputHandler(&printToString<&leadCommandOutput>); doCommand(command); resetOutputHandler(); StringBuffer report; report.appendSprintf("{\"type\":\"reply\",\"from\":%d,\"id\":%lu,\"end\":true,\"reply\":", to, id); report.appendJsonString(leadCommandOutput, true); report += "}\n"; leadSignal(report); leadCommandOutput = (char*)NULL; free(buffer); return; } // we can only send one command at a time if (leadCommandTo) { // TODO we could stop reading the HQ socket in this mode and then never get a busy? free(buffer); return leadCommandError(to,id,"busy"); } // send over mesh to recipient and cache id for any replies leadAnswerID = id; leadCommandTo = to; leadCommandChunks = command; leadCommandChunksAt = 0; leadCommandRetries = 0; leadCommandChunk(); } free(buffer); }
// when we can't process a command for some internal reason void leadCommandError(int from, int id, const char *reason) { StringBuffer err(128); err.appendSprintf("{\"type\":\"reply\",\"from\":%d,\"id\":%d,\"err\":true,\"reply\":\"%s\"}\n", from, id, reason); leadSignal(err); }
// process a packet from HQ void leadIncoming(char *packet, unsigned short *index) { char *type, *command; uint16_t to; unsigned long id; type = j0g_str("type", packet, index); if (hqVerboseOutput) { speol(type); } if (strcmp(type, "online") == 0) { Shell.allReportHQ(); } if (strcmp(type, "command") == 0) { to = atoi(j0g_str("to", packet, index)); id = strtoul(j0g_str("id", packet, index), NULL, 10); command = j0g_str("command", packet, index); if (strlen(j0g_str("to", packet, index)) == 0 || !id || !command) { if (hqVerboseOutput) { speol("invalid command, requires to, id, command"); sp("to: "); speol(to); sp("id: "); speol(id); sp("command: "); speol(command); } return; } // handle internal ones first if (to == Scout.getAddress()) { Shell.bitlashOutput = (char*)malloc(255); sprintf(Shell.bitlashOutput,"{\"type\":\"reply\",\"from\":%d,\"id\":%lu,\"end\":true,\"reply\":\"", to, id); setOutputHandler(&bitlashBuffer); doCommand(command); strcpy(Shell.bitlashOutput + strlen(Shell.bitlashOutput), "\"}\n"); setOutputHandler(&bitlashFilter); leadSignal(Shell.bitlashOutput); free(Shell.bitlashOutput); return; } // we can only send one command at a time if (leadCommandTo) { // TODO we could stop reading the HQ socket in this mode and then never get a busy? return leadCommandError(to,id,"busy"); } // send over mesh to recipient and cache id for any replies leadAnswerID = id; leadCommandTo = to; leadCommandChunks = (char*)malloc(strlen(command) + 1); strcpy(leadCommandChunks, command); leadCommandChunksAt = 0; leadCommandRetries = 0; leadCommandChunk(); } }