C++ Wrapper around the IBM LMiC LoRaWAN implementation

Dependencies:   LMiC SX1276Lib

Committer:
Sille Van Landschoot
Date:
Tue Nov 22 20:43:03 2016 +0100
Revision:
10:9bf05e9b4cde
Parent:
9:cfe697b53d71
Add reset pin support with define directive

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sillevl 1:7cfc59ba0797 1 /* The MIT License (MIT)
sillevl 1:7cfc59ba0797 2 *
sillevl 1:7cfc59ba0797 3 * Copyright (c) 2016 Sille Van Landschoot
sillevl 1:7cfc59ba0797 4 *
sillevl 1:7cfc59ba0797 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
sillevl 1:7cfc59ba0797 6 * of this software and associated documentation files (the "Software"), to deal
sillevl 1:7cfc59ba0797 7 * in the Software without restriction, including without limitation the rights
sillevl 1:7cfc59ba0797 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
sillevl 1:7cfc59ba0797 9 * copies of the Software, and to permit persons to whom the Software is
sillevl 1:7cfc59ba0797 10 * furnished to do so, subject to the following conditions:
sillevl 1:7cfc59ba0797 11 *
sillevl 1:7cfc59ba0797 12 * The above copyright notice and this permission notice shall be included in all
sillevl 1:7cfc59ba0797 13 * copies or substantial portions of the Software.
sillevl 1:7cfc59ba0797 14 *
sillevl 1:7cfc59ba0797 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
sillevl 1:7cfc59ba0797 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
sillevl 1:7cfc59ba0797 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
sillevl 1:7cfc59ba0797 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
sillevl 1:7cfc59ba0797 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sillevl 1:7cfc59ba0797 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
sillevl 1:7cfc59ba0797 21 * SOFTWARE.
sillevl 1:7cfc59ba0797 22 */
sillevl 1:7cfc59ba0797 23
sillevl 1:7cfc59ba0797 24
sillevl 1:7cfc59ba0797 25 #include <Node.h>
sillevl 1:7cfc59ba0797 26 #include <stdio.h>
sillevl 1:7cfc59ba0797 27 #include <stdlib.h>
sillevl 1:7cfc59ba0797 28
sillevl 1:7cfc59ba0797 29 void onEvent(ev_t ev)
sillevl 1:7cfc59ba0797 30 {
sillevl 1:7cfc59ba0797 31 SimpleLoRaWAN::Node::onEvent(ev);
sillevl 1:7cfc59ba0797 32 }
sillevl 1:7cfc59ba0797 33
sillevl 1:7cfc59ba0797 34
sillevl 2:6fac10decfb3 35
sillevl 1:7cfc59ba0797 36
sillevl 1:7cfc59ba0797 37 namespace SimpleLoRaWAN
sillevl 1:7cfc59ba0797 38 {
sillevl 1:7cfc59ba0797 39
Sille Van Landschoot 10:9bf05e9b4cde 40 Node::Node():rfm95wReset(p15)
sillevl 1:7cfc59ba0797 41 {
Sille Van Landschoot 10:9bf05e9b4cde 42 #ifdef RFM95_RESET_CONNECTED
Sille Van Landschoot 10:9bf05e9b4cde 43 rfm95wReset = 0;
Sille Van Landschoot 10:9bf05e9b4cde 44 wait_ms(10);
Sille Van Landschoot 10:9bf05e9b4cde 45 rfm95wReset = 1;
Sille Van Landschoot 10:9bf05e9b4cde 46 wait_ms(10);
Sille Van Landschoot 10:9bf05e9b4cde 47 #endif
sillevl 2:6fac10decfb3 48 init();
sillevl 1:7cfc59ba0797 49 }
sillevl 1:7cfc59ba0797 50
sillevl 1:7cfc59ba0797 51 Node::~Node()
sillevl 1:7cfc59ba0797 52 {
sillevl 1:7cfc59ba0797 53
sillevl 1:7cfc59ba0797 54 }
sillevl 1:7cfc59ba0797 55
sillevl 2:6fac10decfb3 56 void Node::init()
sillevl 2:6fac10decfb3 57 {
sillevl 2:6fac10decfb3 58 os_init();
sillevl 2:6fac10decfb3 59
sillevl 2:6fac10decfb3 60 // reset MAC state
sillevl 2:6fac10decfb3 61 LMIC_reset();
Sille Van Landschoot 10:9bf05e9b4cde 62 setSpreadFactor(DR_SF7);
sillevl 2:6fac10decfb3 63 }
sillevl 2:6fac10decfb3 64
sillevl 1:7cfc59ba0797 65
sillevl 1:7cfc59ba0797 66 void Node::send(char* data, int size)
sillevl 1:7cfc59ba0797 67 {
Sille Van Landschoot 10:9bf05e9b4cde 68 send((uint8_t*) data, size);
Sille Van Landschoot 10:9bf05e9b4cde 69 }
Sille Van Landschoot 10:9bf05e9b4cde 70
Sille Van Landschoot 10:9bf05e9b4cde 71 void Node::send(uint8_t* data, int size)
Sille Van Landschoot 10:9bf05e9b4cde 72 {
sillevl 1:7cfc59ba0797 73 memcpy (LMIC.frame, data, size);
sillevl 1:7cfc59ba0797 74 LMIC_setTxData2(15, LMIC.frame, size, 0);
sillevl 1:7cfc59ba0797 75 }
sillevl 1:7cfc59ba0797 76
sillevl 1:7cfc59ba0797 77
sillevl 1:7cfc59ba0797 78 void Node::onEvent(ev_t ev)
sillevl 1:7cfc59ba0797 79 {
sillevl 1:7cfc59ba0797 80 printf("Event (%d)!!!\r\n", ev);
sillevl 1:7cfc59ba0797 81 switch(ev) {
sillevl 1:7cfc59ba0797 82 case EV_JOINED:
sillevl 1:7cfc59ba0797 83 printf("JOINED\n\r");
sillevl 1:7cfc59ba0797 84 LMIC_setLinkCheckMode(0); // Link check is currently not implemented for TTN, so just disable it
sillevl 1:7cfc59ba0797 85 break;
sillevl 1:7cfc59ba0797 86 case EV_TXCOMPLETE:
sillevl 1:7cfc59ba0797 87 printf("TXCOMPLETE\n\r");
sillevl 1:7cfc59ba0797 88 break;
sillevl 1:7cfc59ba0797 89 default:
sillevl 1:7cfc59ba0797 90 break;
sillevl 1:7cfc59ba0797 91 }
sillevl 1:7cfc59ba0797 92 }
sillevl 1:7cfc59ba0797 93
sillevl 1:7cfc59ba0797 94 void Node::process()
sillevl 1:7cfc59ba0797 95 {
sillevl 1:7cfc59ba0797 96 os_runloop_once();
sillevl 1:7cfc59ba0797 97 }
sillevl 1:7cfc59ba0797 98
Sille Van Landschoot 9:cfe697b53d71 99 void Node::enableLinkCheck()
Sille Van Landschoot 9:cfe697b53d71 100 {
Sille Van Landschoot 9:cfe697b53d71 101 setLinkCheck(true);
Sille Van Landschoot 9:cfe697b53d71 102 }
Sille Van Landschoot 9:cfe697b53d71 103
Sille Van Landschoot 9:cfe697b53d71 104 void Node::disableLinkCheck()
Sille Van Landschoot 9:cfe697b53d71 105 {
Sille Van Landschoot 9:cfe697b53d71 106 setLinkCheck(false);
Sille Van Landschoot 9:cfe697b53d71 107 }
Sille Van Landschoot 9:cfe697b53d71 108
Sille Van Landschoot 9:cfe697b53d71 109 void Node::setLinkCheck(int state)
Sille Van Landschoot 9:cfe697b53d71 110 {
Sille Van Landschoot 9:cfe697b53d71 111 LMIC_setLinkCheckMode(state);
Sille Van Landschoot 9:cfe697b53d71 112 }
Sille Van Landschoot 9:cfe697b53d71 113
Sille Van Landschoot 10:9bf05e9b4cde 114 void Node::setSpreadFactor(int spreadfactor)
Sille Van Landschoot 10:9bf05e9b4cde 115 {
Sille Van Landschoot 10:9bf05e9b4cde 116 LMIC_setDrTxpow(spreadfactor, 14);
Sille Van Landschoot 10:9bf05e9b4cde 117 }
Sille Van Landschoot 10:9bf05e9b4cde 118
sillevl 2:6fac10decfb3 119
sillevl 1:7cfc59ba0797 120 } /* namespace SimpleLoRaWAN */