This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.
Dependencies: NDefLib X_NUCLEO_NFC01A1 mbed
Fork of X-MBED-NFC1 by
This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.
The available demos are:
- SAMPLE_WRITE_URL: write a tag with the ST home page URL
- SAMPLE_COUNT_CLICK: create a custom tag to count and report the user button clicks.
- SAMPLE_WRITE_AND_CHANGE_ALL: write a tag with all the supported records and update the tag contents when the user button is pressed.
- SAMPLE_LOCK_TAG_CONTENT: use the M24SR component API to set the NFC tag as read-only.
To enable the different demos comment/uncomment the SAMPLE_* macros provided in main.cpp .
Diff: Samples/SampleAsync_writeAndChangeAll.cpp
- Revision:
- 17:d8d3d2088cac
- Parent:
- 15:94c98d2aa235
- Child:
- 18:d596eb3f575f
diff -r 02611ca49f48 -r d8d3d2088cac Samples/SampleAsync_writeAndChangeAll.cpp --- a/Samples/SampleAsync_writeAndChangeAll.cpp Wed Aug 31 15:14:08 2016 +0000 +++ b/Samples/SampleAsync_writeAndChangeAll.cpp Thu Jul 13 09:41:01 2017 +0000 @@ -46,13 +46,14 @@ #include "NDefLib/RecordType/RecordText.h" #include "NDefLib/RecordType/RecordMimeType.h" #include "NDefLib/RecordType/RecordVCard.h" +#include "NDefLib/RecordType/RecordWifiConf.h" -#include "X_NUCLEO_NFC01A1.h" +#include "XNucleoNFC01A1.h" /** * Shift the led status between the 3 leds. */ -static void shiftLed(DigitalOut &led1,DigitalOut &led2,DigitalOut &led3){ +static void shift_led(DigitalOut &led1,DigitalOut &led2,DigitalOut &led3){ const uint8_t prevLed1=led1; const uint8_t prevLed2=led2; const uint8_t prevLed3=led3; @@ -65,45 +66,45 @@ * Print in the console some data about the record. * @param r Record to print. */ -static void printRecord(NDefLib::Record *const r){ +static void print_record(NDefLib::Record *const r){ using namespace NDefLib; - switch(r->getType()){ + switch(r->get_type()){ case Record::TYPE_TEXT: { const RecordText *const temp = ( RecordText* )r; - printf("Read Text: %s\r\n",temp->getText().c_str()); + printf("Read Text: %s\r\n",temp->get_text().c_str()); break; } case Record::TYPE_AAR:{ const RecordAAR *const temp = ( RecordAAR* )r; - printf("Read ARR: %s\r\n",temp->getPackage().c_str()); + printf("Read ARR: %s\r\n",temp->get_package().c_str()); break; } case Record::TYPE_MIME:{ const RecordMimeType *const temp = ( RecordMimeType* )r; - printf("Read mimeType: %s\r\n",temp->getMimeType().c_str()); + printf("Read mimeType: %s\r\n",temp->get_mime_type().c_str()); printf("Read mimeData: %s\r\n", - std::string((const char*)temp->getMimeData(), - temp->getMimeDataLenght()).c_str()); + std::string((const char*)temp->get_mime_data(), + temp->get_mime_data_lenght()).c_str()); break;} case Record::TYPE_URI:{ RecordURI *const temp = (RecordURI*)r; - printf("Read uriId: %d\r\n",temp->getUriId()); - printf("Read uriType: %s\r\n",temp->getUriType().c_str()); - printf("Read uriContent: %s\r\n",temp->getContent().c_str()); + printf("Read uriId: %d\r\n",temp->get_uri_id()); + printf("Read uriType: %s\r\n",temp->get_uri_type().c_str()); + printf("Read uriContent: %s\r\n",temp->get_content().c_str()); break;} case Record::TYPE_URI_MAIL:{ const RecordMail*const temp = (RecordMail*)r; - printf("Read Dest: %s\r\n",temp->getToAddress().c_str()); - printf("Read Subject: %s\r\n",temp->getSubject().c_str()); - printf("Read Body: %s\r\n",temp->getBody().c_str()); + printf("Read Dest: %s\r\n",temp->get_to_address().c_str()); + printf("Read Subject: %s\r\n",temp->get_subject().c_str()); + printf("Read Body: %s\r\n",temp->get_body().c_str()); break;} case Record::TYPE_URI_SMS:{ const RecordSMS*const temp = (RecordSMS*)r; - printf("Read number: %s\r\n",temp->getNumber().c_str()); - printf("Read message: %s\r\n",temp->getMessagge().c_str()); + printf("Read number: %s\r\n",temp->get_number().c_str()); + printf("Read message: %s\r\n",temp->get_messagge().c_str()); break;} case Record::TYPE_URI_GEOLOCATION:{ const RecordGeo*const temp = (RecordGeo*)r; - printf("Read lat: %f\r\n",temp->getLatitude()); - printf("Read long: %f\r\n",temp->getLongitude()); + printf("Read lat: %f\r\n",temp->get_latitude()); + printf("Read long: %f\r\n",temp->get_longitude()); break;} case Record::TYPE_MIME_VCARD:{ const RecordVCard *const temp = (RecordVCard*)r; @@ -111,6 +112,13 @@ printf("Read Mail: %s\r\n",(*temp)[RecordVCard::EMAIL_WORK].c_str()); printf("Read ORG: %s\r\n",(*temp)[RecordVCard::ORGANIZATION].c_str()); break;} + case Record::TYPE_WIFI_CONF:{ + const RecordWifiConf *const temp = (RecordWifiConf*)r; + printf("Nework Name: %s\r\n",temp->get_network_ssid().c_str()); + printf("Nework Key: %s\r\n",temp->get_network_key().c_str()); + printf("Nework Auth: %X\r\n",temp->get_auth_type()); + printf("Nework Enc: %X\r\n",temp->get_encryption()); + break;} case Record::TYPE_UNKNOWN:{ printf("Unknown record\r\n"); break;} @@ -121,46 +129,53 @@ * Change the record content. * @param r Record to change. */ -static void changeRecord(NDefLib::Record const* r){ +static void change_record(NDefLib::Record const* r){ using namespace NDefLib; - switch(r->getType()){ + switch(r->get_type()){ case Record::TYPE_TEXT: { RecordText *temp = (RecordText*)r; - temp->setText("Hello"); + temp->set_text("Hello"); break; } case Record::TYPE_AAR:{ RecordAAR *temp = (RecordAAR*)r; - temp->setPackage("set Package Ok"); + temp->set_package("set Package Ok"); break; } case Record::TYPE_MIME:{ RecordMimeType *temp = (RecordMimeType*)r; - temp->copyMimeData((const uint8_t *)"String2",sizeof("String2")); + temp->copy_mime_data((const uint8_t *)"String2",sizeof("String2")); break;} case Record::TYPE_URI:{ RecordURI *temp = (RecordURI*)r; - temp->setContent("mbed.com"); + temp->set_content("mbed.com"); break;} case Record::TYPE_URI_MAIL:{ RecordMail *temp = (RecordMail*)r; - temp->setToAddress("newMail@st.com"); - temp->setSubject("tag change"); - temp->setBody("read/change Works!"); + temp->set_to_address("newMail@st.com"); + temp->set_subject("tag change"); + temp->set_body("read/change Works!"); break;} case Record::TYPE_URI_SMS:{ RecordSMS *temp = (RecordSMS*)r; - temp->setMessage("Message Change"); - temp->setNumber("0987654321"); + temp->set_message("Message Change"); + temp->set_number("0987654321"); break;} case Record::TYPE_URI_GEOLOCATION:{ RecordGeo *temp = (RecordGeo*)r; - temp->setLatitude(-temp->getLatitude()); - temp->setLongitude(-temp->getLongitude()); + temp->set_latitude(-temp->get_latitude()); + temp->set_longitude(-temp->get_longitude()); break;} case Record::TYPE_MIME_VCARD:{ RecordVCard *temp = (RecordVCard*)r; (*temp)[RecordVCard::NAME]="name change"; (*temp)[RecordVCard::NICKNAME]="nic change"; break;} + case Record::TYPE_WIFI_CONF:{ + RecordWifiConf * temp = (RecordWifiConf*)r; + temp->set_network_ssid("hackMe"); + temp->set_network_key("qwerty"); + temp->set_auth_type(RecordWifiConf::AUTH_WPA2_PSK); + temp->set_encryption_type(RecordWifiConf::ENC_TYPE_AES_TKIP); + break;} case Record::TYPE_UNKNOWN:{ printf("Unknown record\r\n"); break;} @@ -177,7 +192,7 @@ NDefLib::Message *mMsg; /// Message where read bool *mDisableButton; /// enable the user button - void onFinish(char *msg){ + void on_finish(const char *const msg){ printf(msg); *mDisableButton=false; } @@ -193,9 +208,9 @@ /** * Create the message and ask to read it */ - virtual void onSessionOpen(NDefLib::NDefNfcTag *tag,bool success){ + virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){ if(!success){ - return onFinish("Error Opening Session\r\n"); + return on_finish("Error Opening Session\r\n"); }//if mMsg = new NDefLib::Message; tag->read(mMsg); @@ -204,31 +219,31 @@ /** * Print all the record inside the message */ - virtual void onMessageRead(NDefLib::NDefNfcTag *tag,bool success, + virtual void on_message_read(NDefLib::NDefNfcTag *tag,bool success, const NDefLib::Message *readMsg){ - if(!success || readMsg->getNRecords()==0){ + if(!success || readMsg->get_N_records()==0){ delete mMsg; - return onFinish("Error Reading\r\n"); + return on_finish("Error Reading\r\n"); }else{ printf("Message Read\r\n\n"); - for(uint32_t i=0;i<readMsg->getNRecords();i++){ + for(uint32_t i=0;i<readMsg->get_N_records();i++){ NDefLib::Record *r = (*readMsg)[i]; - printRecord(r); + print_record(r); delete r; }//for delete mMsg; }//if-else - tag->closeSession(); + tag->close_session(); } /** * Enable the button */ - virtual void onSessionClose(NDefLib::NDefNfcTag *,bool success){ + virtual void on_session_close(NDefLib::NDefNfcTag *,bool success){ if(success) - onFinish("Read Session close\r\n"); + on_finish("Read Session close\r\n"); else - onFinish("Read Session close Error\r\n"); + on_finish("Read Session close Error\r\n"); } }; @@ -245,17 +260,17 @@ * Change all the record in the message. * @param readMsg Message to change. */ - static void changeContent(const NDefLib::Message *readMsg){ - for(uint32_t i=0;i<readMsg->getNRecords();i++){ - changeRecord((*readMsg)[i]); + static void change_content(const NDefLib::Message *readMsg){ + for(uint32_t i=0;i<readMsg->get_N_records();i++){ + change_record((*readMsg)[i]); }//for }//changeContent /** * Delete all the record and the message. */ - void deleteMessage(){ - for(uint32_t i=0;i<mMsg->getNRecords();i++){ + void delete_message(){ + for(uint32_t i=0;i<mMsg->get_N_records();i++){ delete (*mMsg)[i]; }//for delete mMsg; @@ -272,7 +287,7 @@ /** * Ask to read the tag content */ - virtual void onSessionOpen(NDefLib::NDefNfcTag *tag,bool success){ + virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){ if(!success){ printf("Error Opening the session"); return; @@ -284,14 +299,14 @@ /** * Change the message content and write it back */ - virtual void onMessageRead(NDefLib::NDefNfcTag *tag,bool success, + virtual void on_message_read(NDefLib::NDefNfcTag *tag,bool success, const NDefLib::Message *readMsg){ - if(!success || readMsg->getNRecords()==0){ + if(!success || readMsg->get_N_records()==0){ printf("Error Reading\r\n"); - deleteMessage(); + delete_message(); }else{ printf("Message Read: change message content\r\n"); - changeContent(readMsg); + change_content(readMsg); printf("Start write new message\r\n"); tag->write(*mMsg); }//if-else @@ -300,12 +315,12 @@ /** * Delete the Message and close the session */ - virtual void onMessageWrite(NDefLib::NDefNfcTag *tag,bool success, + virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success, const NDefLib::Message &){ - deleteMessage(); + delete_message(); if(success){ printf("Message Wrote\r\n"); - tag->closeSession(); + tag->close_session(); }else printf("Error Writing\r\n"); }//onMessageWrite @@ -313,11 +328,11 @@ /** * Set the callback for print the tag content and open a new session */ - virtual void onSessionClose(NDefLib::NDefNfcTag *tag,bool success){ + virtual void on_session_close(NDefLib::NDefNfcTag *tag,bool success){ if(success){ printf("Change Session close\r\n"); - tag->setCallback(mReadMessage); - tag->openSession(); + tag->set_callback(mReadMessage); + tag->open_session(); }else printf("Change Session close Error\r\n"); } @@ -343,7 +358,7 @@ /** * */ - virtual void onSessionOpen(NDefLib::NDefNfcTag *tag,bool success){ + virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){ if(!success){ printf("Error Opening the Session\r\n"); return; @@ -355,25 +370,28 @@ NDefLib::Message msg; NDefLib::RecordAAR rAAR("com.st.BlueMS"); - msg.addRecord(&rAAR); + msg.add_record(&rAAR); NDefLib::RecordSMS rSMS("123456789","st.com.BlueMS"); - msg.addRecord(&rSMS); + msg.add_record(&rSMS); NDefLib::RecordGeo rGeo(123.123,-456.789); - msg.addRecord(&rGeo); + msg.add_record(&rGeo); NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com"); - msg.addRecord(&rUri); + msg.add_record(&rUri); NDefLib::RecordMail rMail("mail@st.com","ciao","da nfc tag"); - msg.addRecord(&rMail); + msg.add_record(&rMail); NDefLib::RecordMimeType rText1("text/plain",(const uint8_t*)"Ciao",4); - msg.addRecord(&rText1); + msg.add_record(&rText1); NDefLib::RecordText rText3(NDefLib::RecordText::UTF8,"it","ciao"); - msg.addRecord(&rText3); + msg.add_record(&rText3); + + NDefLib::RecordWifiConf rWifi("OpenNetwork"); + msg.add_record(&rWifi); NDefLib::RecordVCard::VCardInfo_t cardInfo; cardInfo[NDefLib::RecordVCard::FORMATTED_NAME]="prova prova1"; @@ -394,7 +412,7 @@ cardInfo[NDefLib::RecordVCard::URL]="www.st.com"; cardInfo[NDefLib::RecordVCard::PHOTO_URI]="http://www.st.com/st-web-ui/static/active/en/fragment/multimedia/image/picture/customer_focus.jpg"; NDefLib::RecordVCard rVCard(cardInfo); - msg.addRecord(&rVCard); + msg.add_record(&rVCard); //write it tag->write(msg); @@ -404,23 +422,23 @@ /** * Close the session */ - virtual void onMessageWrite(NDefLib::NDefNfcTag *tag,bool success, + virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success, const NDefLib::Message &){ if(!success) printf("Error Writing\r\n"); printf("Message wrote\r\n"); - tag->closeSession(); + tag->close_session(); } /** * Set the callback to print the tag content and open a new session */ - virtual void onSessionClose(NDefLib::NDefNfcTag *tag,bool success){ + virtual void on_session_close(NDefLib::NDefNfcTag *tag,bool success){ if(success){ printf("Write Session close\r\n"); - tag->setCallback(mReadMessage); - tag->openSession(); + tag->set_callback(mReadMessage); + tag->open_session(); }else printf("Write Session close Error\r\n"); }//onSessionClose @@ -433,7 +451,7 @@ /** * Call back called when the user press the button */ -static void setButtonPress(){ +static void set_button_press(){ buttonPress=true; }//if buttonPress @@ -442,9 +460,9 @@ /** * Call back called when the user press the button */ -static void setNfcEvent(){ +static void set_nfc_event(){ nfcEvent=true; -}//if buttonPress +}// /** * Write a message and when the user press the button it read the message, change it and update it. @@ -453,45 +471,51 @@ DigitalOut nucleoLed(LED1); //instance the board with the default parameters - I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); - X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,&setNfcEvent); + I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN); + XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,&set_nfc_event); //retrieve the Nfc component - M24SR &nfc = nfcNucleo->getM24SR(); + M24SR &nfc = nfcNucleo->get_M24SR(); //retrieve the NdefLib interface - NDefLib::NDefNfcTag& tag = nfc.getNDefTag(); + NDefLib::NDefNfcTag& tag = nfc.get_NDef_tag(); //switch on the first led - nfcNucleo->getLed1()=1; - nfcNucleo->getLed2()=0; - nfcNucleo->getLed3()=0; + nfcNucleo->get_led1()=1; + nfcNucleo->get_led2()=0; + nfcNucleo->get_led3()=0; ReadMessageCallback readMessageCallback(&buttonPress); WriteMessageCallback writeBigMessageCallback(&readMessageCallback); ChangeMessageCallback changeMessageCallback(&readMessageCallback); //Enable async mode - if(nfcNucleo->getM24SR().GetSession()!=NFC_SUCCESS) - nfcNucleo->getM24SR().ManageI2CGPO(I2C_ANSWER_READY); + if(nfc.get_session()==M24SR::M24SR_SUCCESS) + nfc.manage_I2C_GPO(M24SR::I2C_ANSWER_READY); //write the message - tag.setCallback(&writeBigMessageCallback); - tag.openSession(); + tag.set_callback(&writeBigMessageCallback); + tag.open_session(); //enable the button - InterruptIn mybutton(USER_BUTTON); - mybutton.fall(setButtonPress); + #if defined(TARGET_STM) + InterruptIn userButton(USER_BUTTON); + #else + InterruptIn userButton(SW2); + #endif + userButton.fall(set_button_press); + printf("Start main loop\r\n"); while(true) { if(buttonPress){ - shiftLed(nfcNucleo->getLed1(),nfcNucleo->getLed2(),nfcNucleo->getLed3()); - tag.setCallback(&changeMessageCallback); - tag.openSession(); + buttonPress=false; + shift_led(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3()); + tag.set_callback(&changeMessageCallback); + tag.open_session(); }else if (nfcEvent){ + nfcEvent=false; nucleoLed=!nucleoLed; - nfcNucleo->getM24SR().ManageEvent(); + nfcNucleo->get_M24SR().manage_event(); }//if-else __WFE(); }//while -} - +} \ No newline at end of file