Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NFC library #783

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions libraries/nfc/examples/SendText/SendText.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
SendText.ino

Written by Chiara Ruggeri ([email protected])

This example for the Arduino Primo board shows how to use
NFC library.
It sets a text message specifying the language code, then
starts the module, so that when a device with NFC is near
to the board the message "Hello World!" will be sent.

This example code is in the public domain.

*/

#include <NFC.h>

void setup() {
// set the NFC message as first parameter and the language code as second
NFC.setTXTmessage("Hello World!", "en");
// start the NFC module
NFC.start();
}

void loop() {

}
62 changes: 62 additions & 0 deletions libraries/nfc/examples/SendURL/SendURL.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
SendURL.ino

Written by Chiara Ruggeri ([email protected])

This example for the Arduino Primo board shows how to use
NFC library.
It sets an URI message and then starts the module, so that
when a device with NFC is near to the board the message
http://www.arduino.org will be sent.
A list of all possible URI message can be found here:

NFC_URI_HTTP_WWW "http://www."
NFC_URI_HTTPS_WWW "https://www."
NFC_URI_HTTP "http:"
NFC_URI_HTTPS "https:"
NFC_URI_TEL "tel:"
NFC_URI_MAILTO "mailto:"
NFC_URI_FTP_ANONYMOUS "ftp://anonymous:anonymous@"
NFC_URI_FTP_FTP "ftp://ftp."
NFC_URI_FTPS "ftps://"
NFC_URI_SFTP, "sftp://"
NFC_URI_SMB "smb://"
NFC_URI_NFS "nfs://"
NFC_URI_FTP "ftp://"
NFC_URI_DAV "dav://"
NFC_URI_NEWS "news:"
NFC_URI_TELNET "telnet://"
NFC_URI_IMAP "imap:"
NFC_URI_RTSP "rtsp://"
NFC_URI_URN "urn:"
NFC_URI_POP "pop:"
NFC_URI_SIP "sip:"
NFC_URI_SIPS "sips:"
NFC_URI_TFTP "tftp:"
NFC_URI_BTSPP "btspp://"
NFC_URI_BTL2CAP "btl2cap://"
NFC_URI_BTGOEP "btgoep://"
NFC_URI_TCPOBEX "tcpobex://"
NFC_URI_IRDAOBEX "irdaobex://"
NFC_URI_FILE "file://"
NFC_URI_URN_EPC_ID "urn:epc:id:"
NFC_URI_URN_EPC_TAG "urn:epc:tag:"
NFC_URI_URN_EPC_PAT "urn:epc:pat:"
NFC_URI_URN_EPC_RAW "urn:epc:raw:"
NFC_URI_URN_EPC "urn:epc:"
NFC_URI_URN_NFC "urn:nfc:"

This example code is in the public domain.

*/

#include <NFC.h>

void setup() {
NFC.setURImessage("arduino.org", NFC_URI_HTTP_WWW);
NFC.start();
}


void loop() {
}
60 changes: 60 additions & 0 deletions libraries/nfc/examples/StartApp/StartApp.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
SendText.ino

Written by Chiara Ruggeri ([email protected])

This example for the Arduino Primo board shows how to use
NFC library.
It sets a app message specifying the package name (for Android)
and the ID application (for Windows phone), then starts the
module, so that when a device with NFC is near to the board
it will try to open the application (if present) or will
look for the app in the store. Finally it register a callback
function that will be called any time an NFC field is detected
(it means that a device is near).

This example code is in the public domain.

*/

#include <NFC.h>

//specify the package name for windows and android phone and insert the EOL character at the end '\0'
static const char android_package_name[] = {'n', 'o', '.', 'n', 'o', 'r', 'd', 'i', 'c', 's',
'e', 'm', 'i', '.', 'a', 'n', 'd', 'r', 'o', 'i',
'd', '.', 'n', 'r', 'f', 't', 'o', 'o', 'l', 'b',
'o', 'x', '\0'};

static const char windows_application_id[] = {'{', 'e', '1', '2', 'd', '2', 'd', 'a', '7', '-',
'4', '8', '8', '5', '-', '4', '0', '0', 'f', '-',
'b', 'c', 'd', '4', '-', '6', 'c', 'b', 'd', '5',
'b', '8', 'c', 'f', '6', '2', 'c', '}', '\0'};

void setup() {
Serial.begin(9600);
NFC.setAPPmessage(android_package_name, windows_application_id);
NFC.start();
NFC.registerCallback(myFunction);
}


void loop() {
}

void myFunction(void *context, nfc_t2t_event_t event, const uint8_t *data, size_t dataLength){
(void)context;

switch (event)
{
case NFC_T2T_EVENT_FIELD_ON:
Serial.println("******NFC_T2T_EVENT_FIELD_ON******");
break;

case NFC_T2T_EVENT_FIELD_OFF:
Serial.println("------NFC_T2T_EVENT_FIELD_OFF------");
break;

default:
break;
}
}
24 changes: 24 additions & 0 deletions libraries/nfc/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#######################################
# Syntax Coloring Map NFC
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

NFC KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
start KEYWORD2
stop KEYWORD2
setTXTmessage KEYWORD2
setURImessage KEYWORD2
setAPPmessage KEYWORD2
setOobPairingKey KEYWORD2
registerCallback KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
11 changes: 11 additions & 0 deletions libraries/nfc/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name=NFC
version=1.0
author=Arduino
maintainer=Arduino <[email protected]>
sentence=Allows Arduino Primo boards to use the NFC tag onboard
paragraph=
category=Communication
url=http://www.arduino.org/learning/reference/NFC
includes=NFC.h
architectures=nrf52
precompiled=true
158 changes: 158 additions & 0 deletions libraries/nfc/src/NFC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
NFC class for nRF52.
Written by Chiara Ruggeri ([email protected])

Copyright (c) 2016 Arduino. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Enjoy!
*/


#include "NFC.h"
#include <Arduino.h>
void nfc_callback(void *context, nfc_t2t_event_t event, const uint8_t *data, size_t dataLength)
{
NFC.onService(context,event,data,dataLength);
switch (event) {
case NFC_T2T_EVENT_FIELD_ON:
pinMode(LED_RED, OUTPUT);
digitalWrite(LED_RED, LOW);
break;
case NFC_T2T_EVENT_FIELD_OFF:
pinMode(LED_RED, OUTPUT);
digitalWrite(LED_RED, HIGH);
break;
default:
break;
}
}


uint8_t ndef_msg_buf[256];

void NFCClass::setTXTmessage(const char TXTMessage[], const char language[]){

nfc_t2t_setup(nfc_callback, NULL);
uint32_t len = sizeof(ndef_msg_buf);
uint8_t sizeM=strlen(TXTMessage);
uint8_t sizeL=strlen(language);

NFC_NDEF_MSG_DEF(welcome_msg, 1);
NFC_NDEF_TEXT_RECORD_DESC_DEF(en_text_rec,
UTF_8,
(uint8_t *)language,
sizeL,
(uint8_t *)TXTMessage,
sizeM);
nfc_ndef_msg_record_add( &NFC_NDEF_MSG(welcome_msg),
&NFC_NDEF_TEXT_RECORD_DESC(en_text_rec));

nfc_ndef_msg_encode(&NFC_NDEF_MSG(welcome_msg),
ndef_msg_buf,
&len);

nfc_t2t_payload_set((uint8_t *) ndef_msg_buf, len);
//Serial.println("Send");
}

void NFCClass::setURImessage( const char URL[], nfc_uri_id_t type){

uint8_t size=strlen(URL);
memset(ndef_msg_buf, 0, 256);

uint32_t len = sizeof(ndef_msg_buf);
nfc_t2t_setup(nfc_callback, NULL);
nfc_uri_msg_encode( type,
(uint8_t *) URL,
size,
ndef_msg_buf,
&len);

nfc_t2t_payload_set((uint8_t*)ndef_msg_buf, len);

}

void NFCClass::setAPPmessage(const char android_app[], const char windows_app[]){

uint8_t sizeA=strlen(android_app);
uint8_t sizeW=strlen(windows_app);
uint32_t len = sizeof(ndef_msg_buf);

nfc_t2t_setup(nfc_callback, NULL);
nfc_launchapp_msg_encode((uint8_t *)android_app,
sizeA,
(uint8_t *)windows_app,
sizeW,
ndef_msg_buf,
&len);

nfc_t2t_payload_set((uint8_t *) ndef_msg_buf, len);
}

// void NFCClass::setOobPairingKey(){

// uint8_t key[16];
// uint8_t random_values_length, rand_values, generated;
// static ble_advdata_tk_value_t oob_auth_key;
// uint32_t len=sizeof(ndef_msg_buf);

// //try to generate the key randomly
// sd_rand_application_pool_capacity_get(&random_values_length);
// //we need only 16 bytes
// if(random_values_length>16)
// random_values_length=16;
// //wait until values are generated
// do
// sd_rand_application_bytes_available_get(&generated);
// while(generated<random_values_length);
// //get the random data
// sd_rand_application_vector_get(key, random_values_length);
// //if random values are less than 16 add static data to fill the buffer
// if(random_values_length<16)
// for(int i=random_values_length; i<16; i++)
// key[i]=i;

// memcpy(oob_auth_key.tk, key, 16);

// nfc_t2t_setup(nfc_callback, NULL);
// nfc_ble_pair_default_msg_encode(NFC_BLE_PAIR_MSG_FULL,
// &oob_auth_key,
// ndef_msg_buf,
// &len);

// nfc_t2t_payload_set((char *) ndef_msg_buf, len);
// }

void NFCClass::start(){
nfc_t2t_emulation_start();
}

void NFCClass::stop(){
nfc_t2t_emulation_stop();
}

void NFCClass::registerCallback(void(*function)(void *context, nfc_t2t_event_t event, const uint8_t *data, size_t dataLength)){
Callback=function;
}

void NFCClass::onService(void *context, nfc_t2t_event_t event, const uint8_t *data, size_t dataLength){
if(Callback)
Callback(context,event,data,dataLength);
}


NFCClass NFC;
Loading
Loading