Sille Van Landschoot / Simple-LoRaWAN

Dependencies:   LMiC SX1276Lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Node.cpp Source File

Node.cpp

00001 /* The MIT License (MIT)
00002  *
00003  * Copyright (c) 2016 Sille Van Landschoot
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in all
00013  * copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00021  * SOFTWARE.
00022  */
00023 
00024 
00025 #include <Node.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 
00029 void onEvent(ev_t ev)
00030 {
00031     SimpleLoRaWAN::Node::onEvent(ev);
00032 }
00033 
00034 
00035 
00036 
00037 namespace SimpleLoRaWAN
00038 {
00039 
00040 Node::Node():rfm95wReset(p15)
00041 {
00042 #ifdef RFM95_RESET_CONNECTED
00043     rfm95wReset = 0;
00044     wait_ms(10);
00045     rfm95wReset = 1;
00046     wait_ms(10);
00047 #endif
00048     init();
00049 }
00050 
00051 Node::~Node()
00052 {
00053 
00054 }
00055 
00056 void Node::init()
00057 {
00058     os_init();
00059 
00060     // reset MAC state
00061     LMIC_reset();
00062     setSpreadFactor(DR_SF7);
00063 }
00064 
00065 
00066 void Node::send(char* data, int size)
00067 {
00068     send((uint8_t*) data, size);
00069 }
00070 
00071 void Node::send(uint8_t* data, int size)
00072 {
00073     memcpy (LMIC.frame, data, size);
00074     LMIC_setTxData2(15, LMIC.frame, size, 0);
00075 }
00076 
00077 
00078 void Node::onEvent(ev_t ev)
00079 {
00080     printf("Event (%d)!!!\r\n", ev);
00081     switch(ev) {
00082         case EV_JOINED:
00083             printf("JOINED\n\r");
00084             LMIC_setLinkCheckMode(0); // Link check is currently not implemented for TTN, so just disable it
00085             break;
00086         case EV_TXCOMPLETE:
00087             printf("TXCOMPLETE\n\r");
00088             break;
00089         default:
00090             break;
00091     }
00092 }
00093 
00094 void Node::process()
00095 {
00096     os_runloop_once();
00097 }
00098 
00099 void Node::enableLinkCheck()
00100 {
00101     setLinkCheck(true);
00102 }
00103 
00104 void Node::disableLinkCheck()
00105 {
00106     setLinkCheck(false);
00107 }
00108 
00109 void Node::setLinkCheck(int state)
00110 {
00111     LMIC_setLinkCheckMode(state);
00112 }
00113 
00114 void Node::setSpreadFactor(int spreadfactor)
00115 {
00116     LMIC_setDrTxpow(spreadfactor, 14);
00117 }
00118 
00119 
00120 } /* namespace SimpleLoRaWAN */