Machine Cloud
MachineCloud.cpp@0:260d7796eb25, 2013-08-09 (annotated)
- Committer:
- ddollar
- Date:
- Fri Aug 09 19:29:51 2013 +0000
- Revision:
- 0:260d7796eb25
- Child:
- 1:f5eafb42e9e4
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ddollar | 0:260d7796eb25 | 1 | #include <string.h> |
ddollar | 0:260d7796eb25 | 2 | #include "MachineCloud.h" |
ddollar | 0:260d7796eb25 | 3 | |
ddollar | 0:260d7796eb25 | 4 | MachineCloud::MachineCloud() { |
ddollar | 0:260d7796eb25 | 5 | init(0x1000); |
ddollar | 0:260d7796eb25 | 6 | } |
ddollar | 0:260d7796eb25 | 7 | |
ddollar | 0:260d7796eb25 | 8 | MachineCloud::MachineCloud(uint16_t router) { |
ddollar | 0:260d7796eb25 | 9 | init(router); |
ddollar | 0:260d7796eb25 | 10 | } |
ddollar | 0:260d7796eb25 | 11 | |
ddollar | 0:260d7796eb25 | 12 | void MachineCloud::init(uint16_t router) { |
ddollar | 0:260d7796eb25 | 13 | m_router = router; |
ddollar | 0:260d7796eb25 | 14 | } |
ddollar | 0:260d7796eb25 | 15 | |
ddollar | 0:260d7796eb25 | 16 | int MachineCloud::connect() { |
ddollar | 0:260d7796eb25 | 17 | m_xbee = new XBee(p9, p10); |
ddollar | 0:260d7796eb25 | 18 | return 0; |
ddollar | 0:260d7796eb25 | 19 | } |
ddollar | 0:260d7796eb25 | 20 | |
ddollar | 0:260d7796eb25 | 21 | int MachineCloud::disconnect() { |
ddollar | 0:260d7796eb25 | 22 | m_xbee = NULL; |
ddollar | 0:260d7796eb25 | 23 | return 0; |
ddollar | 0:260d7796eb25 | 24 | } |
ddollar | 0:260d7796eb25 | 25 | |
ddollar | 0:260d7796eb25 | 26 | int MachineCloud::on_receive(MachineCloudCallback *function) { |
ddollar | 0:260d7796eb25 | 27 | m_receiverFunction = function; |
ddollar | 0:260d7796eb25 | 28 | m_receiverObject = NULL; |
ddollar | 0:260d7796eb25 | 29 | return 0; |
ddollar | 0:260d7796eb25 | 30 | } |
ddollar | 0:260d7796eb25 | 31 | |
ddollar | 0:260d7796eb25 | 32 | int MachineCloud::on_receive(MachineCloudReceiver *object) { |
ddollar | 0:260d7796eb25 | 33 | m_receiverFunction = NULL; |
ddollar | 0:260d7796eb25 | 34 | m_receiverObject = object; |
ddollar | 0:260d7796eb25 | 35 | return 0; |
ddollar | 0:260d7796eb25 | 36 | } |
ddollar | 0:260d7796eb25 | 37 | |
ddollar | 0:260d7796eb25 | 38 | int MachineCloud::send(char *key, char *value) { |
ddollar | 0:260d7796eb25 | 39 | m_lock.lock(); |
ddollar | 0:260d7796eb25 | 40 | |
ddollar | 0:260d7796eb25 | 41 | int size = strlen(key) + strlen(value) + 1; |
ddollar | 0:260d7796eb25 | 42 | char *message = (char *)malloc(size); |
ddollar | 0:260d7796eb25 | 43 | sprintf(message, "%s=%s", key, value); |
ddollar | 0:260d7796eb25 | 44 | |
ddollar | 0:260d7796eb25 | 45 | uint8_t payload[size]; |
ddollar | 0:260d7796eb25 | 46 | for (int i=0; i<size; i++) { |
ddollar | 0:260d7796eb25 | 47 | payload[i] = message[i]; |
ddollar | 0:260d7796eb25 | 48 | } |
ddollar | 0:260d7796eb25 | 49 | |
ddollar | 0:260d7796eb25 | 50 | free(message); |
ddollar | 0:260d7796eb25 | 51 | |
ddollar | 0:260d7796eb25 | 52 | TxStatusResponse txStatus = TxStatusResponse(); |
ddollar | 0:260d7796eb25 | 53 | Tx16Request tx = Tx16Request(m_router, payload, sizeof(payload)); |
ddollar | 0:260d7796eb25 | 54 | m_xbee->send(tx); |
ddollar | 0:260d7796eb25 | 55 | |
ddollar | 0:260d7796eb25 | 56 | return 0; |
ddollar | 0:260d7796eb25 | 57 | } |
ddollar | 0:260d7796eb25 | 58 | |
ddollar | 0:260d7796eb25 | 59 | void MachineCloud::receive(char *key, char *value) { |
ddollar | 0:260d7796eb25 | 60 | if (m_receiverFunction) (*m_receiverFunction)(key, value); |
ddollar | 0:260d7796eb25 | 61 | if (m_receiverObject) m_receiverObject->receive(key, value); |
ddollar | 0:260d7796eb25 | 62 | } |
ddollar | 0:260d7796eb25 | 63 | |
ddollar | 0:260d7796eb25 | 64 | Rx16Response rx16 = Rx16Response(); |
ddollar | 0:260d7796eb25 | 65 | |
ddollar | 0:260d7796eb25 | 66 | int MachineCloud::loop() { |
ddollar | 0:260d7796eb25 | 67 | m_xbee->readPacket(); |
ddollar | 0:260d7796eb25 | 68 | |
ddollar | 0:260d7796eb25 | 69 | if (m_xbee->getResponse().isAvailable()) { |
ddollar | 0:260d7796eb25 | 70 | switch (m_xbee->getResponse().getApiId()) { |
ddollar | 0:260d7796eb25 | 71 | case RX_16_RESPONSE: |
ddollar | 0:260d7796eb25 | 72 | Rx16Response rx16 = Rx16Response(); |
ddollar | 0:260d7796eb25 | 73 | m_xbee->getResponse().getRx16Response(rx16); |
ddollar | 0:260d7796eb25 | 74 | char *data = (char *)malloc(rx16.getDataLength() + 1); |
ddollar | 0:260d7796eb25 | 75 | for (int i=0; i<rx16.getDataLength(); i++) { |
ddollar | 0:260d7796eb25 | 76 | data[i] = rx16.getData(i); |
ddollar | 0:260d7796eb25 | 77 | } |
ddollar | 0:260d7796eb25 | 78 | data[rx16.getDataLength()] = 0; |
ddollar | 0:260d7796eb25 | 79 | char *key = strtok(data, "="); |
ddollar | 0:260d7796eb25 | 80 | char *val = strtok(NULL, "="); |
ddollar | 0:260d7796eb25 | 81 | receive(key, val); |
ddollar | 0:260d7796eb25 | 82 | free(data); |
ddollar | 0:260d7796eb25 | 83 | } |
ddollar | 0:260d7796eb25 | 84 | } |
ddollar | 0:260d7796eb25 | 85 | |
ddollar | 0:260d7796eb25 | 86 | |
ddollar | 0:260d7796eb25 | 87 | return 0; |
ddollar | 0:260d7796eb25 | 88 | } |