ST / M24SR

Dependencies:   ST_INTERFACES

Dependents:   X_NUCLEO_NFC01A1

Fork of M24SR by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers M24SR.h Source File

M24SR.h

00001 /**
00002  ******************************************************************************
00003  * @file    m24sr_class.h
00004  * @author  ST Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   This file provides a set of functions to interface with the M24SR
00008  *          device.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 /* Revision ------------------------------------------------------------------*/
00040 /*
00041  Based on:         X-CUBE-MEMS1/trunk/Drivers/BSP/Components/m24sr/m24sr.h
00042  Revision:         M24SR Driver V1.0.0
00043  */
00044 
00045 /* Define to prevent recursive inclusion -------------------------------------*/
00046 
00047 #ifndef __M24SR_CLASS_H
00048 #define __M24SR_CLASS_H
00049 
00050 /* Includes ------------------------------------------------------------------*/
00051 
00052 #include "mbed.h"
00053 #include "I2C.h"
00054 
00055 #include "m24sr_def.h"
00056 #include "Nfc.h"
00057 
00058 #include "NDefLib/NDefNfcTag.h"
00059 class NDefNfcTagM24SR;
00060 
00061 /* Classes -------------------------------------------------------------------*/
00062 
00063 /**
00064 * Class representing a M24SR component.
00065 * This component has two operation modes, sync or async.
00066 * In sync mode each function call returns only after the command has completed.
00067 * In async mode each function call returns immediately and the answer will be notified
00068 * through a callback.
00069 * The default behavior is sync mode.
00070 * To enable the async mode ManageI2CGPO(I2C_ANSWER_READY) function must be called.
00071 * When the component notifies an interrupt user must call  {@link ManageEvent} function.
00072 * Note that passing a parameter other than I2C_ANSWER_READY to ManageI2CGPO initialize the component in sync mode.
00073  */
00074 class M24SR: public Nfc {
00075 
00076 public:
00077 
00078     typedef enum {
00079         M24SR_SUCCESS=NFC_SUCCESS,
00080         M24SR_ERROR=0x6F00,
00081         M24SR_FILE_OVERFLOW_LE = 0x6280,
00082         M24SR_EOF = 0x6282,
00083         M24SR_PASSWORD_REQUIRED = 0x6300,
00084         M24SR_PASSWORD_INCORRECT = 0x63C0,
00085         M24SR_PASSWORD_INCORRECT1RETRY = 0x63C1,
00086         M24SR_PASSWORD_INCORRECT2RETRY = 0x63C2,
00087         M24SR_WRONG_LENGHT = 0x6700,
00088         M24SR_UNSUCESSFUL_UPDATING = 0x6581,
00089         M24SR_INCOPATIBLE_COMMAND= 0x6981,
00090         M24SR_SECURITY_UNSATISFIED = 0x6982,
00091         M24SR_REFERENCE_DATA_NOT_USABLE = 0x6984,
00092 
00093         M24SR_INCORRECT_PARAMETER = 0x6a80,
00094         M24SR_FILE_NOT_FOUND=0x6a82,
00095         M24SR_FILE_OVERFLOW_LC = 0x6A84, //TODO difference with Le??
00096 
00097         M24SR_INCORRECT_P1_OR_P2 = 0x6A86, //TODO better name?
00098         M24SR_RF_SESSION_KILLED=0x6500,
00099         M24SR_INS_NOT_SUPPORTED=0x6D00,
00100         M24SR_CLASS_NOT_SUPPORTED=0x6E00,
00101 
00102         //IOError
00103         M24SR_IO_ERROR_I2CTIMEOUT=0x0011,
00104         M24SR_IO_ERROR_CRC=0x0012,
00105         M24SR_IO_ERROR_NACK=0x0013,
00106         M24SR_IO_ERROR_PARAMETER=0x0014,
00107         M24SR_IO_ERROR_NBATEMPT=0x0015,
00108         M24SR_IO_NOACKNOWLEDGE=0x0016,
00109         M24SR_IO_PIN_NOT_CONNECTED=0x0017
00110     } StatusTypeDef;
00111 
00112     /**
00113      * @brief  GPO state structure
00114      */
00115     typedef enum {
00116         HIGH_IMPEDANCE = 0,
00117         SESSION_OPENED =1,
00118         WIP=2,
00119         I2C_ANSWER_READY=3,
00120         INTERRUPT=4,
00121         STATE_CONTROL=5
00122     } NFC_GPO_MGMT;
00123 
00124     /**
00125      * Possible password to set.
00126      */
00127     typedef enum{
00128         ReadPwd,   //!< Password to use before reading the tag
00129         WritePwd,  //!< Password to use before writing the tag
00130         I2CPwd,    //!< Root password, used only through nfc
00131     }PasswordType_t;
00132 
00133     /**
00134      * Default password used to change the write/read permission
00135      */
00136     static const uint8_t DEFAULT_PASSWORD[16];
00137 
00138     /**
00139      * Default gpo status -> the gpo will remain high
00140      */
00141     static const NFC_GPO_MGMT DEFAULT_GPO_STATUS=HIGH_IMPEDANCE;
00142 
00143 
00144     /**
00145      * Function that will be called when an interrupt is fired,
00146      * this function must be set if you want use the component in async mode.
00147      */
00148     typedef void(*gpoEventCallback)(void);
00149 
00150     /**
00151      * Object that contains all the callbacks fired by this class, each command has its own callback.
00152      * The callback default implementation is an empty function.
00153      */
00154     class Callbacks{
00155         public:
00156 
00157             /** called when GetSession or ForceGetSession completes
00158              * @see M24SR#GetSession
00159              * @see M24SR#ForceGetSession */
00160             virtual void on_session_open(M24SR *nfc,StatusTypeDef status) {
00161                 (void)nfc; (void)status;
00162             }
00163 
00164             /** called when Deselect completes
00165              * @see M24SR#Deselect */
00166             virtual void on_deselect(M24SR *nfc,StatusTypeDef status) {
00167                 (void)nfc; (void)status;
00168             }
00169 
00170             /** called when SelectedApplication completes
00171              * @see M24SR#SelectedApplication */
00172             virtual void on_selected_application(M24SR *nfc,StatusTypeDef status) {
00173                 (void)nfc; (void)status;
00174             }
00175 
00176             /** called when SelectedCCFile completes
00177              * @see M24SR#SelectedCCFile */
00178             virtual void on_selected_CC_file(M24SR *nfc,StatusTypeDef status) {
00179                 (void)nfc; (void)status;
00180             }
00181 
00182             /** called when SelectedNDEFFile completes
00183              * @see M24SR#SelectedNDEFFile */
00184             virtual void on_selected_NDEF_file(M24SR *nfc,StatusTypeDef status) {
00185                 (void)nfc; (void)status;
00186             }
00187 
00188             /** called when SelectedSystemFile completes
00189              * @see M24SR#SelectedSystemFile */
00190             virtual void on_selected_system_file(M24SR *nfc,StatusTypeDef status) {
00191                 (void)nfc; (void)status;
00192             }
00193 
00194             /** called when read_binary or STReadBinary completes
00195              * @see M24SR#read_binary
00196              * @see M24SR#STReadBinary */
00197             virtual void on_read_byte(M24SR *nfc,StatusTypeDef status,
00198                     uint16_t offset,uint8_t *readByte, uint16_t nReadByte) {
00199                 (void)nfc; (void)status;(void)offset; (void)readByte; (void)nReadByte;
00200             }
00201 
00202             /** called when UpdatedBinary completes
00203              * @see M24SR#UpdatedBinary */
00204             virtual void on_updated_binary(M24SR *nfc,StatusTypeDef status,uint16_t offset,
00205                     uint8_t *writeByte,uint16_t nWriteByte) {
00206                 (void)nfc; (void)status; (void)writeByte; (void)nWriteByte; (void)offset;
00207             }
00208 
00209             /** called when Verify completes
00210              * @see M24SR#Verify */
00211             virtual void on_verified(M24SR *nfc,StatusTypeDef status,PasswordType_t uPwdId,
00212                     const uint8_t *pwd) {
00213                 (void)nfc; (void)status;(void)uPwdId;(void)pwd;
00214             }
00215 
00216             /** called when ManageI2CGPO completes
00217              * @see M24SR#ManageI2CGPO */
00218             virtual void on_manage_I2C_GPO(M24SR *nfc,StatusTypeDef status,NFC_GPO_MGMT newStatus) {
00219                 (void)nfc; (void)status;(void)newStatus;
00220             }
00221 
00222             /** called when ManageRFGPO completes
00223              * @see M24SR#ManageRFGPO */
00224             virtual void on_manage_RF_GPO(M24SR *nfc,StatusTypeDef status,NFC_GPO_MGMT newStatus) {
00225                 (void)nfc; (void)status;(void)newStatus;
00226             }
00227 
00228             /** called when ChangeReferenceData completes
00229              * @see M24SR#ChangeReferenceData */
00230             virtual void on_change_reference_data(M24SR *nfc ,StatusTypeDef status,PasswordType_t type,
00231                     uint8_t *data) {
00232                 (void)nfc; (void)status;(void)type;(void)data;
00233             }
00234 
00235             /** called when EnableVerificationRequirement completes
00236              * @see M24SR#EnableVerificationRequirement */
00237             virtual void on_enable_verification_requirement(M24SR *nfc ,StatusTypeDef status,PasswordType_t type) {
00238                 (void)nfc; (void)status;(void)type;
00239             }
00240 
00241             /** called when disable_verification_requirement completes
00242              * @see M24SR#disable_verification_requirement */
00243             virtual void on_disable_verification_requirement(M24SR *nfc , StatusTypeDef status,PasswordType_t type) {
00244                 (void)nfc; (void)status;(void)type;
00245             }
00246 
00247             /** called when EnablePermanentState completes
00248              * @see M24SR#EnablePermanentState */
00249             virtual void on_enable_permanent_state(M24SR *nfc, StatusTypeDef status, PasswordType_t type) {
00250                 (void)nfc; (void)status;(void)type;
00251             }
00252 
00253             /** called when DisablePermanentState completes
00254              * @see M24SR#DisablePermanentState */
00255             virtual void on_disable_permanent_state(M24SR *nfc, StatusTypeDef status, PasswordType_t type) {
00256                 (void)nfc; (void)status;(void)type;
00257             }
00258 
00259             /** called when ReadId completes
00260              * @see M24SR#ReadId */
00261             virtual void on_read_id(M24SR *nfc, StatusTypeDef status, uint8_t *id) {
00262                         (void)nfc; (void)status;(void)id;
00263             }
00264 
00265             /** called when EnableReadPassword completes
00266              * @see M24SR#EnableReadPassword */
00267             virtual void on_enable_read_password(M24SR *nfc, StatusTypeDef status,const uint8_t *newPwd) {
00268                 (void)nfc; (void)status;(void)newPwd;
00269             }
00270 
00271             /** called when EnableWritePassword completes
00272              * @see M24SR#EnableWritePassword */
00273             virtual void on_enable_write_password(M24SR *nfc, StatusTypeDef status,const uint8_t *newPwd) {
00274                 (void)nfc; (void)status;(void)newPwd;
00275             }
00276 
00277             /** called when DisableReadPassword completes
00278              * @see M24SR#DisableReadPassword */
00279             virtual void on_disable_read_password(M24SR *nfc, StatusTypeDef status) {
00280                 (void)nfc; (void)status;
00281             }
00282 
00283             /** called when DisableWritePassword completes
00284              * @see M24SR#DisableWritePassword */
00285             virtual void on_disable_write_password(M24SR *nfc, StatusTypeDef status) {
00286                 (void)nfc; (void)status;
00287             }
00288 
00289             /** called when DisableAllPassword completes
00290              * @see M24SR#DisableAllPassword */
00291             virtual void on_disable_all_password(M24SR *nfc, StatusTypeDef status) {
00292                 (void)nfc; (void)status;
00293             }
00294 
00295 
00296             /** called when EnableReadOnly completes
00297              * @see M24SR#EnableReadOnly */
00298             virtual void on_enable_read_only(M24SR *nfc,StatusTypeDef status) {
00299                 (void)nfc; (void)status;
00300             }
00301 
00302             /** called when EnableWriteOnly completes
00303              * @see M24SR#EnableWriteOnly */
00304             virtual void on_enable_write_only(M24SR *nfc,StatusTypeDef status) {
00305                 (void)nfc; (void)status;
00306             }
00307 
00308 
00309             /** called when DisableReadOnly completes
00310              * @see M24SR#DisableReadOnly */
00311             virtual void on_disable_read_only(M24SR *nfc,StatusTypeDef status) {
00312                 (void)nfc; (void)status;
00313             }
00314 
00315             /** called when DisableWriteOnly completes
00316              * @see M24SR#DisableWriteOnly */
00317             virtual void on_disable_write_only(M24SR *nfc,StatusTypeDef status) {
00318                 (void)nfc; (void)status;
00319             }
00320 
00321             virtual ~Callbacks() {};
00322         };
00323 
00324     /*** Constructor and Destructor Methods ***/
00325 
00326     /**
00327      * @brief Constructor.
00328      * @param address I2C address of the component.
00329      * @param I2C     I2C device to be used for communication.
00330      * @param eventCallback Function that will be called when the gpo pin status changes.
00331      * @param GPOPinName Pin used as GPIO.
00332      * @param RFDISPinName Pin used to disable the RF function.
00333      */
00334     M24SR(const uint8_t address, I2C &I2C,gpoEventCallback eventCallback, const PinName& GPOPinName,
00335             const PinName& RFDISPinName);
00336 
00337     /**
00338      * @brief Destructor.
00339      */
00340     virtual ~M24SR(void);
00341 
00342     /*** Public Component Related Methods ***/
00343     
00344     /**
00345      * @brief  This function initializes the M24SR device.
00346      * @param ptr Configure parameters, not used.
00347      * @return M24SR_SUCCESS if no errors
00348      */
00349     virtual int init(void *ptr) {
00350         return (StatusTypeDef) M24SR_Init((M24SR_InitTypeDef*)ptr);
00351     }
00352 
00353     virtual int read_id(uint8_t *id) {
00354         return (StatusTypeDef) M24SR_ReadID((uint8_t *) id);
00355     }
00356 
00357     /** lock the tag channel */
00358     virtual StatusTypeDef get_session(void) {
00359         return (StatusTypeDef) M24SR_GetSession();
00360     }
00361 
00362     /**
00363      * Force to open an I2C connection , abort the RF connection.
00364      * @return M24SR_SUCCESS if the session is acquired
00365      */
00366     StatusTypeDef force_get_session(void) {
00367         return (StatusTypeDef) M24SR_ForceSession();
00368     }
00369 
00370     virtual StatusTypeDef deselect(void) {
00371         return (StatusTypeDef) M24SR_Deselect();
00372     }
00373 
00374     ////////// tag4 command ////////////////////
00375     
00376     /**
00377      * Select the application file.
00378      * @return M24SR_SUCCESS if the application is selected
00379      */
00380     StatusTypeDef select_application(void) {
00381         return M24SR_SendSelectApplication();
00382     }
00383 
00384     /**
00385      * Select the CC file.
00386      * @return M24SR_SUCCESS if the CC file is selected.
00387      */
00388     StatusTypeDef select_CC_file(void) {
00389         return M24SR_SendSelectCCfile();
00390     }
00391 
00392     /**
00393      * Select the NDEF file.
00394      * @param NDEFfileId File id to open.
00395      * @return M24SR_SUCCESS if the file is selected
00396      */
00397     StatusTypeDef select_NDEF_file(uint16_t NDEFfileId) {
00398         return M24SR_SendSelectNDEFfile(NDEFfileId);
00399     }
00400 
00401     /**
00402      * Select the system file.
00403      * @return M24SR_SUCCESS if the system file is selected
00404      */
00405     StatusTypeDef select_system_file(void) {
00406         return M24SR_SendSelectSystemfile();
00407     }
00408 
00409     virtual int read_binary(uint16_t Offset, uint8_t NbByteToRead,
00410             uint8_t *pBufferRead) {
00411         return (StatusTypeDef) M24SR_SendReadBinary((uint16_t) Offset,
00412                 (uint8_t) NbByteToRead, (uint8_t *) pBufferRead);
00413     }
00414 
00415     virtual int update_binary(uint16_t Offset,
00416             uint8_t NbByteToWrite,uint8_t *pDataToWrite) {
00417         return (StatusTypeDef) M24SR_SendUpdateBinary((uint16_t) Offset,
00418                 (uint8_t) NbByteToWrite, (uint8_t *) pDataToWrite);
00419     }
00420 
00421     /////////////////// iso 7816-4 commands //////////////////////////
00422     
00423     virtual StatusTypeDef verify(PasswordType_t pwdId, uint8_t NbPwdByte,
00424             const uint8_t *pPwd) {
00425         return (StatusTypeDef) M24SR_SendVerify(passwordTypeToConst(pwdId),
00426                 (uint8_t) NbPwdByte, pPwd);
00427     }
00428 
00429     virtual StatusTypeDef change_reference_data(PasswordType_t pwdId,
00430             const uint8_t *pPwd) {
00431         return (StatusTypeDef) M24SR_SendChangeReferenceData(passwordTypeToConst(pwdId),
00432                 (uint8_t *) pPwd);
00433     }
00434 
00435     virtual StatusTypeDef enable_verification_requirement(
00436             PasswordType_t uReadOrWrite) {
00437         return (StatusTypeDef) M24SR_SendEnableVerificationRequirement(
00438                 passwordTypeToConst(uReadOrWrite));
00439     }
00440 
00441     virtual StatusTypeDef disable_verification_requirement(
00442             PasswordType_t uReadOrWrite) {
00443         return (StatusTypeDef) M24SR_SendDisableVerificationRequirement(
00444                 passwordTypeToConst(uReadOrWrite));
00445     }
00446 
00447     ////////////////////// ST proprietary //////////////////////////////////
00448 
00449     /**
00450      * Same as {@link NFC#read_binary}, however permitting to read more bytes than available.
00451      * @param Offset read offset.
00452      * @param NbByteToRead Number of bytes to read.
00453      * @param[out] pBufferRead Buffer to store the read data into.
00454      * @return M24SR_SUCCESS if no errors
00455      */
00456     StatusTypeDef ST_read_binary(uint16_t Offset,
00457             uint8_t NbByteToRead, uint8_t *pBufferRead) {
00458         return (StatusTypeDef) M24SR_SendSTReadBinary((uint16_t) Offset,
00459                 (uint8_t) NbByteToRead, (uint8_t *) pBufferRead);
00460     }
00461 
00462     virtual StatusTypeDef enable_permanent_state(PasswordType_t uReadOrWrite) {
00463         return (StatusTypeDef) M24SR_SendEnablePermanentState(
00464                 passwordTypeToConst(uReadOrWrite));
00465     }
00466 
00467     virtual StatusTypeDef disable_permanent_state(PasswordType_t uReadOrWrite) {
00468         return (StatusTypeDef) M24SR_SendDisablePermanentState(
00469                 passwordTypeToConst(uReadOrWrite));
00470     }
00471 
00472     ///////////////////// chip configuration /////////////////////////////////
00473     /**
00474      * Set the gpo output pin.
00475      * @param uSetOrReset New pin status.
00476      * @return M24SR_SUCCESS if no errors
00477      */
00478     StatusTypeDef state_control(uint8_t uSetOrReset) {
00479         return (StatusTypeDef) M24SR_StateControl((uint8_t) uSetOrReset);
00480     }
00481 
00482     /**
00483      * @brief  This function configures GPO for I2C session.
00484      * @param  GPO_I2Cconfig GPO configuration to set.
00485      * @return M24SR_SUCCESS if no errors
00486      * @par if the configuration is I2C_ANSWER_READY, the component will start to work
00487      * in async mode.
00488      */
00489     StatusTypeDef manage_I2C_GPO(NFC_GPO_MGMT GPO_I2Cconfig) {
00490         return (StatusTypeDef) M24SR_ManageI2CGPO(GPO_I2Cconfig);
00491     }
00492 
00493 
00494     /**
00495     * @brief  This function configures GPO for RF session.
00496     * @param  GPO_RFconfig GPO configuration to set.
00497     * @return M24SR_SUCCESS if no errors
00498     */
00499     StatusTypeDef manage_RF_GPO(uint8_t GPO_RFconfig) {
00500         return (StatusTypeDef) M24SR_ManageRFGPO(
00501                 (NFC_GPO_MGMT) GPO_RFconfig);
00502     }
00503 
00504 
00505     /**
00506      * @brief  This function enables or disables the RF communication.
00507      * @param  OnOffChoice GPO configuration to set.
00508      * @return M24SR_SUCCESS if no errors
00509      */
00510 
00511     StatusTypeDef RF_config(uint8_t OnOffChoice) {
00512         return (StatusTypeDef) M24SR_RFConfig((uint8_t) OnOffChoice);
00513     }
00514 
00515 
00516     /**
00517      * Generates a negative pulse on the GPO pin.
00518      * Pulse starts immediately after the command is issued and ends at the end of the RF response.
00519      * @return M24SR_SUCCESS if no errors
00520      */
00521     StatusTypeDef send_interrupt(void) {
00522         return (StatusTypeDef) M24SR_SendInterrupt();
00523     }
00524 
00525 
00526     /**
00527      * Function to call when the component fire an interrupt.
00528      * @return last operation status
00529      */
00530     StatusTypeDef manage_event();
00531 
00532     /**
00533      * Change the function to call when a command ends.
00534      * @param commandCallback Object containing the callback, if NULL it will use empty callback
00535      */
00536     void set_callback(Callbacks *commandCallback) {
00537         if (commandCallback!=NULL) {
00538             mCallback = commandCallback;
00539         } else {
00540             mCallback = &defaultCallback;
00541         }
00542     }
00543 
00544 
00545     /////////////////// hight level/utility function /////////////////////
00546 
00547 
00548     /**
00549      * Enable the request of a password before reading the tag.
00550      * @param  pCurrentWritePassword Current password
00551      * @param  pNewPassword Password to request before reading the tag.
00552      * @return return M24SR_SUCCESS if no errors
00553      * @par The password must have a length of 16 chars.
00554      */
00555     StatusTypeDef enable_read_password(const uint8_t* pCurrentWritePassword, const uint8_t* pNewPassword) {
00556 
00557         //enable the callback for change the gpo
00558         mComponentCallback = &mChangePasswordRequestStatusCallback;
00559         mChangePasswordRequestStatusCallback.set_task(ReadPwd,pNewPassword);
00560 
00561         return verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
00562     }
00563 
00564     /**
00565      * Disable the request of a password before reading the tag.
00566      * @param  pCurrentWritePassword Current password
00567      * @return return M24SR_SUCCESS if no errors
00568      * @par The password must have a length of 16 chars.
00569      */
00570     StatusTypeDef disable_read_password(const uint8_t* pCurrentWritePassword) {
00571         mComponentCallback = &mChangePasswordRequestStatusCallback;
00572         mChangePasswordRequestStatusCallback.set_task(ReadPwd,NULL);
00573 
00574         return verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
00575     }
00576 
00577     /**
00578      * Enable the request of a password before writing to the tag.
00579      * @param  pCurrentWritePassword Current password
00580      * @param  pNewPassword Password to request before reading the tag.
00581      * @return return M24SR_SUCCESS if no errors
00582      * @par The password must have a length of 16 chars.
00583      */
00584     StatusTypeDef enable_write_password(const uint8_t* pCurrentWritePassword, uint8_t* pNewPassword) {
00585         //enable the callback for change the gpo
00586         mComponentCallback = &mChangePasswordRequestStatusCallback;
00587         mChangePasswordRequestStatusCallback.set_task(WritePwd,pNewPassword);
00588 
00589         return verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
00590     }
00591 
00592     /**
00593      * Disable the request of a password before writing the tag.
00594      * @param  pCurrentWritePassword Current password.
00595      * @return return M24SR_SUCCESS if no errors
00596      * @par The password must have a length of 16 chars.
00597      */
00598     StatusTypeDef disable_write_password(const uint8_t* pCurrentWritePassword) {
00599         mComponentCallback = &mChangePasswordRequestStatusCallback;
00600         mChangePasswordRequestStatusCallback.set_task(WritePwd,NULL);
00601 
00602         return verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
00603     }
00604 
00605     /**
00606      * @brief   This function disables both read and write passwords.
00607      * @param   pSuperUserPassword I2C super user password.
00608      * @return  return M24SR_SUCCESS if no errors
00609      * @par The password must have a length of 16 chars.
00610      */
00611     StatusTypeDef disable_all_password(const uint8_t* pSuperUserPassword) {
00612         mComponentCallback = &mRemoveAllPasswordCallback;
00613         return verify(M24SR::I2CPwd, 0x10, pSuperUserPassword);
00614     }
00615 
00616     /**
00617      * @brief   This function enables read only mode.
00618      * @param   pCurrentWritePassword Write password is needed to enable read only mode.
00619      * @return  return M24SR_SUCCESS if no errors
00620      * @par The password must have a length of 16 chars.
00621      */
00622     StatusTypeDef enable_read_only(const uint8_t* pCurrentWritePassword) {
00623         mComponentCallback = &mChangeAccessStateCallback;
00624         //disable write = read only
00625         mChangeAccessStateCallback.change_access_state(ChangeAccessStateCallback::WRITE,false);
00626 
00627         return verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
00628     }
00629 
00630     /**
00631      * @brief   This function disables read only mode.
00632      * @param   pCurrentWritePassword Write password is needed to disable read only mode.
00633      * @return  return M24SR_SUCCESS if no errors
00634      * @par The password must have a length of 16 chars.
00635      */
00636     StatusTypeDef disable_read_only(const uint8_t* pCurrentWritePassword) {
00637         mComponentCallback = &mChangeAccessStateCallback;
00638         mChangeAccessStateCallback.change_access_state(ChangeAccessStateCallback::WRITE,true);
00639 
00640         return verify(M24SR::I2CPwd, 0x10,pCurrentWritePassword);
00641     }
00642 
00643 
00644     /**
00645      * @brief   This function enables write only mode.
00646      * @param   pCurrentWritePassword Write password is needed to enable write only mode.
00647      * @return  return M24SR_SUCCESS if no errors
00648      * @par The password must have a length of 16 chars.
00649      */
00650     StatusTypeDef enable_write_only(const uint8_t* pCurrentWritePassword) {
00651         mComponentCallback = &mChangeAccessStateCallback;
00652         //disable read = enable write only
00653         mChangeAccessStateCallback.change_access_state(ChangeAccessStateCallback::READ,false);
00654 
00655         return verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
00656     }
00657 
00658     /**
00659      * @brief   This function disables write only mode.
00660      * @param   pCurrentWritePassword Write password is needed to disable write only mode.
00661      * @return  return M24SR_SUCCESS if no errors
00662      * @par The password must have a length of 16 chars.
00663      */
00664     StatusTypeDef disable_write_only(const uint8_t* pCurrentWritePassword) {
00665         mComponentCallback = &mChangeAccessStateCallback;
00666         mChangeAccessStateCallback.change_access_state(ChangeAccessStateCallback::READ,true);
00667 
00668         return verify(M24SR::I2CPwd, 0x10, pCurrentWritePassword);
00669     }
00670 
00671 
00672     /**
00673      * Get an implementation of NDefNfcTag to use the library NDefLib.
00674      * @return an object of type NdefNfcTag 
00675      */
00676     NDefLib::NDefNfcTag& get_NDef_tag();
00677 
00678 
00679 protected:
00680 
00681     /*** Protected Component Related Methods ***/
00682 
00683     StatusTypeDef M24SR_Init(M24SR_InitTypeDef *);
00684     StatusTypeDef M24SR_ReadID(uint8_t *nfc_id);
00685     StatusTypeDef M24SR_GetSession(void);
00686     StatusTypeDef M24SR_ForceSession(void);
00687 
00688     StatusTypeDef M24SR_Deselect(void);
00689     StatusTypeDef M24SR_ReceiveDeselect(void);
00690 
00691     StatusTypeDef M24SR_SendSelectApplication(void);
00692     StatusTypeDef M24SR_ReceiveSelectApplication(void);
00693 
00694     StatusTypeDef M24SR_SendSelectCCfile(void);
00695     StatusTypeDef M24SR_ReceiveSelectCCfile(void);
00696 
00697     StatusTypeDef M24SR_SendSelectNDEFfile(uint16_t NDEFfileId);
00698     StatusTypeDef M24SR_ReceiveSelectNDEFfile();
00699 
00700     StatusTypeDef M24SR_SendSelectSystemfile(void);
00701     StatusTypeDef M24SR_ReceiveSelectSystemfile(void);
00702 
00703     StatusTypeDef M24SR_SendReadBinary(uint16_t Offset, uint8_t NbByteToRead,
00704             uint8_t *pBufferRead);
00705     StatusTypeDef M24SR_SendSTReadBinary(uint16_t Offset, uint8_t NbByteToRead,
00706                 uint8_t *pBufferRead);
00707     StatusTypeDef M24SR_ReceiveReadBinary(void);
00708 
00709     StatusTypeDef M24SR_SendUpdateBinary(uint16_t Offset, uint8_t NbByteToWrite,
00710             uint8_t *pDataToWrite);
00711     StatusTypeDef M24SR_ReceiveUpdateBinary();
00712 
00713     StatusTypeDef M24SR_SendVerify(uint16_t uPwdId, uint8_t NbPwdByte,
00714             const uint8_t *pPwd);
00715     StatusTypeDef M24SR_ReceiveVerify();
00716 
00717     StatusTypeDef M24SR_SendChangeReferenceData(uint16_t uPwdId, uint8_t *pPwd);
00718     StatusTypeDef M24SR_ReceiveChangeReferenceData();
00719 
00720     StatusTypeDef M24SR_SendEnableVerificationRequirement(
00721             uint16_t uReadOrWrite);
00722     StatusTypeDef M24SR_ReceiveEnableVerificationRequirement();
00723 
00724     StatusTypeDef M24SR_SendDisableVerificationRequirement(
00725             uint16_t uReadOrWrite);
00726     StatusTypeDef M24SR_ReceiveDisableVerificationRequirement();
00727 
00728     StatusTypeDef M24SR_SendEnablePermanentState(uint16_t uReadOrWrite);
00729     StatusTypeDef M24SR_ReceiveEnablePermanentState();
00730 
00731     StatusTypeDef M24SR_SendDisablePermanentState(uint16_t uReadOrWrite);
00732     StatusTypeDef M24SR_ReceiveDisablePermanentState();
00733 
00734     StatusTypeDef M24SR_SendInterrupt(void);
00735     StatusTypeDef M24SR_StateControl(uint8_t uSetOrReset);
00736 
00737     StatusTypeDef M24SR_ManageI2CGPO(NFC_GPO_MGMT GPO_I2Cconfig);
00738     StatusTypeDef M24SR_ManageRFGPO(NFC_GPO_MGMT GPO_RFconfig);
00739 
00740     StatusTypeDef M24SR_RFConfig(uint8_t OnOffChoice);
00741     StatusTypeDef M24SR_SendFWTExtension(uint8_t FWTbyte);
00742 
00743     
00744     /**
00745      * Send a command to the component.
00746      * @param NbByte Length of the command.
00747      * @param pBuffer Buffer containing the command.
00748      * @return M24SR_SUCCESS if no errors
00749      */
00750     StatusTypeDef M24SR_IO_SendI2Ccommand(uint8_t NbByte, uint8_t *pBuffer);
00751     
00752     /**
00753      * Read a command response.
00754      * @param NbByte Number of bytes to read.
00755      * @param pBuffer Buffer to store the response into.
00756      * @return M24SR_SUCCESS if no errors
00757      */
00758     StatusTypeDef M24SR_IO_ReceiveI2Cresponse(uint8_t NbByte, uint8_t *pBuffer);
00759 
00760     /**
00761      * Do an active polling on the I2C bus until the answer is ready.
00762      * @return M24SR_SUCCESS if no errors
00763      */
00764     StatusTypeDef M24SR_IO_PollI2C(void);
00765 
00766     /**
00767      * Read the gpo pin.
00768      * @param[out] pPinState Variable to store the pin state into.
00769      */
00770     void M24SR_IO_GPO_ReadPin(uint8_t *pPinState) {
00771         *pPinState = GPOPin.read();
00772     }
00773 
00774     /**
00775      * Write the gpo pin.
00776      * @param pPinState Pin state to write.
00777      */
00778     void M24SR_IO_RFDIS_WritePin(uint8_t PinState) {
00779         if (PinState == 0) {
00780             RFDisablePin = 0;
00781         }
00782         else {
00783             RFDisablePin = 1;
00784         }
00785     }
00786 
00787     /*** Component's Instance Variables ***/
00788 
00789     /* Identity */
00790     uint8_t who_am_i;
00791 
00792     /* Type */
00793     uint8_t type;
00794 
00795     /* I2C address */
00796     uint8_t address;
00797 
00798     /* IO Device. */
00799     I2C &dev_I2C;
00800 
00801     /* GPIO */
00802     DigitalIn GPOPin;
00803 
00804     /**
00805      * Pin used to disable the rf chip functionality.
00806      */
00807     DigitalOut RFDisablePin;
00808 
00809     /**
00810      * Buffer used to build the command to send to the chip.
00811      */
00812     uint8_t uM24SRbuffer[0xFF];//max command length is 255
00813 
00814     /**
00815      * ???
00816      */
00817     uint8_t uDIDbyte;
00818 
00819 
00820     /**
00821      * Command that the component can accept
00822      */
00823     typedef enum{
00824         NONE,                            //!< NONE
00825         DESELECT,                        //!< DESELECT
00826         SELECT_APPLICATION,              //!< SELECT_APPLICATION
00827         SELECT_CC_FILE,                  //!< SELECT_CC_FILE
00828         SELECT_NDEF_FILE,                //!< SELECT_NDEF_FILE
00829         SELECT_SYSTEM_FILE,              //!< SELECT_SYSTEM_FILE
00830         READ,                            //!< READ
00831         UPDATE,                          //!< UPDATE
00832         VERIFY,                          //!< VERIFY
00833         MANAGE_I2C_GPO,                  //!< MANAGE_I2C_GPO
00834         MANAGE_RF_GPO,                   //!< MANAGE_RF_GPO
00835         CHANGE_REFERENCE_DATA,           //!< CHANGE_REFERENCE_DATA
00836         ENABLE_VERIFICATION_REQUIREMENT, //!< ENABLE_VERIFICATION_REQUIREMENT
00837         DISABLE_VERIFICATION_REQUIREMENT,//!< DISABLE_VERIFICATION_REQUIREMENT
00838         ENABLE_PERMANET_STATE,           //!< ENABLE_PERMANET_STATE
00839         DISABLE_PERMANET_STATE,          //!< DISABLE_PERMANET_STATE
00840     }M24SR_command_t;
00841 
00842     /**
00843      * User parameter used to invoke a command,
00844      * it is used to provide the data back with the response
00845      */
00846     typedef struct{
00847         uint8_t *data; //!< data
00848         uint16_t length; //!< number of bytes in the data array
00849         uint16_t offset; //!< offset parameter used in the read/write command
00850     }M24SR_command_data_t;
00851 
00852     /**
00853      * Communication mode used by this device
00854      */
00855     typedef enum{
00856         SYNC,//!< SYNC wait the command response before returning
00857         ASYNC//!< ASYNC use a callback to notify the end of a command
00858     }M24SR_communication_t;
00859 
00860     /**
00861      * Type of communication being used
00862      */
00863     M24SR_communication_t mCommunicationType;
00864 
00865     /**
00866      * Last pending command
00867      */
00868     M24SR_command_t mLastCommandSend;
00869 
00870     /**
00871      * Parameter used to invoke the last command
00872      */
00873     M24SR_command_data_t mLastCommandData;
00874 
00875     /**
00876      * Interrupt object fired when the gpo status changes
00877      */
00878     InterruptIn mGpoEventInterrupt;
00879 
00880     /** object containing the callbacks to use*/
00881     Callbacks *mCallback;
00882 
00883     /**
00884      * Object with private callbacks used to hide high level commands each
00885      * calling multiple low level commands. This callbacks object has
00886      * higher priority comparing to the user callbacks.
00887      */
00888     Callbacks *mComponentCallback;
00889 
00890     /**
00891      * Object implementing the interface to use the NDefLib.
00892      */
00893     NDefNfcTagM24SR *mNDefTagUtil;
00894 
00895     /**
00896      * get the callback object to use
00897      * @return callback object to use
00898      */
00899     Callbacks * getCallback() {
00900         if (mComponentCallback != NULL) {
00901             return mComponentCallback;
00902         }
00903         return mCallback;
00904     }//getCallback
00905 
00906 private:
00907     /** object containing empty callback to use in the default case*/
00908     Callbacks defaultCallback;
00909 
00910 
00911     /**
00912      * This class permits to enable/disable the password request to read/write into the tag
00913      * This class is equivalent to calling the methods:
00914      * To enable the request:
00915      * <ul>
00916      *   <li> Verify </li>
00917      *   <li> ChangeReferenceData </li>
00918      *   <li> EnablePermanentState </li>
00919      * </ul>
00920      * To disable the request:
00921      * <ul>
00922      *   <li> Verify </li>
00923      *   <li> disable_verification_requirement </li>
00924      * </ul>
00925      */
00926     class ChangePasswordRequestStatusCallback : public Callbacks {
00927 
00928         public:
00929 
00930             /**
00931              * Build the chain of callbacks.
00932              */
00933             ChangePasswordRequestStatusCallback():
00934                 mNewPwd(NULL), mType(I2CPwd),mEnable(false) {}
00935 
00936             /**
00937              * Set the password to enable/disable.
00938              * @param type Type of password to enable/disable.
00939              * @param newPwd Array of 16bytes with the new password, if null the request will be disabled.
00940              */
00941             void set_task(PasswordType_t type, const uint8_t *newPwd) {
00942                 mNewPwd=newPwd;
00943                 mType=type;
00944                 mEnable=newPwd!=NULL;
00945             }//setTask
00946 
00947             virtual void on_verified(M24SR *nfc, StatusTypeDef status,PasswordType_t ,
00948                     const uint8_t *) {
00949                 if (status!=M24SR_SUCCESS) {
00950                     return onFinishCommand(nfc,status);
00951                 }
00952                 if (mEnable) {
00953                     nfc->change_reference_data(mType,mNewPwd);
00954                 } else {
00955                     nfc->disable_verification_requirement(mType);
00956                 }
00957             }
00958 
00959             virtual void on_disable_verification_requirement(M24SR *nfc,
00960                     StatusTypeDef status, PasswordType_t ) {
00961                 onFinishCommand(nfc,status);
00962             }
00963 
00964             virtual void on_change_reference_data(M24SR *nfc, StatusTypeDef status,
00965                     PasswordType_t type,
00966                     uint8_t *) {
00967                 if (status==M24SR_SUCCESS) {
00968                     nfc->enable_permanent_state(type);
00969                 } else {
00970                     onFinishCommand(nfc,status);
00971                 }
00972             }
00973 
00974             virtual void on_enable_permanent_state(M24SR *nfc, StatusTypeDef status,
00975                     PasswordType_t ) {
00976                 onFinishCommand(nfc,status);
00977             }
00978 
00979 
00980         private:
00981 
00982             /**
00983              * Remove the private callbacks and call the user callback.
00984              * @param nfc Object triggering the command.
00985              * @param status Command status.
00986              */
00987             void onFinishCommand(M24SR *nfc,StatusTypeDef status) {
00988                 nfc->mComponentCallback=NULL;
00989 
00990                 if (mEnable) {
00991                     if (mType==ReadPwd) {
00992                         nfc->getCallback()->on_enable_read_password(nfc,status,mNewPwd);
00993                     } else {
00994                         nfc->getCallback()->on_enable_write_password(nfc,status,mNewPwd);
00995                     }
00996                 } else {
00997                     if (mType==ReadPwd) {
00998                         nfc->getCallback()->on_disable_read_password(nfc,status);
00999                     } else {
01000                         nfc->getCallback()->on_disable_write_password(nfc,status);
01001                     }
01002                 }//if-else enable
01003             }//onFinish
01004 
01005             const uint8_t *mNewPwd;
01006             PasswordType_t mType;
01007             bool mEnable;
01008 
01009     };
01010 
01011     /**
01012      * Object containing the callback chain needed to change the password request
01013      */
01014     ChangePasswordRequestStatusCallback mChangePasswordRequestStatusCallback;
01015     friend class ChangePasswordRequestStatusCallback;
01016 
01017     /**
01018      * This class permits to disable all the password requests to read/write into the tag
01019      * This class is equivalent to calling the methods:
01020      * <ul>
01021      *   <li> Verify(i2c) </li>
01022      *   <li> DisablePermanentState(Read) </li>
01023      *   <li> DisablePermanentState(write) </li>
01024      *   <li> disable_verification_requirement(Read) </li>
01025      *   <li> disable_verification_requirement(write) </li>
01026      *   <li> ChangeReferenceData(Read) </li>
01027      *   <li> ChangeReferenceData(write) </li>
01028      * </ul>
01029      */
01030     class RemoveAllPasswordCallback : public Callbacks{
01031 
01032         /**
01033          * Store the default password used for open a super user session
01034          * it will be set as default read/write password
01035          */
01036         const uint8_t *mI2CPwd;
01037 
01038         public:
01039 
01040             /**
01041              * Build the chain of callbacks.
01042              */
01043             RemoveAllPasswordCallback():mI2CPwd(NULL) {}
01044 
01045             virtual void on_verified(M24SR *nfc,StatusTypeDef status,
01046                     PasswordType_t,const uint8_t* data) {
01047                 if (status!=M24SR_SUCCESS) {
01048                     return onFinishCommand(nfc,status);
01049                 }
01050                 mI2CPwd = data;
01051                 nfc->disable_permanent_state(ReadPwd);
01052             }
01053 
01054             virtual void on_disable_permanent_state(M24SR *nfc , StatusTypeDef status,
01055                     PasswordType_t type) {
01056                 if (status!=M24SR_SUCCESS) {
01057                     return onFinishCommand(nfc,status);
01058                 }
01059                 if (type==ReadPwd) {
01060                     nfc->disable_permanent_state(WritePwd);
01061                 } else {
01062                     nfc->disable_verification_requirement(ReadPwd);
01063                 }
01064             }
01065 
01066             virtual void on_disable_verification_requirement(M24SR *nfc ,
01067                     StatusTypeDef status,PasswordType_t type) {
01068                 if (status!=M24SR_SUCCESS) {
01069                     return onFinishCommand(nfc,status);
01070                 }
01071                 if (type==ReadPwd) {
01072                     nfc->disable_verification_requirement(WritePwd);
01073                 } else {
01074                     nfc->change_reference_data(ReadPwd,mI2CPwd);
01075                 }
01076             }
01077 
01078             virtual void on_change_reference_data(M24SR *nfc ,StatusTypeDef status,PasswordType_t type,
01079                     uint8_t *data) {
01080                 if (status!=M24SR_SUCCESS) {
01081                     return onFinishCommand(nfc,status);
01082                 }
01083                 if (type==ReadPwd) {
01084                     nfc->change_reference_data(WritePwd,data);
01085                 } else {
01086                     onFinishCommand(nfc,status);
01087                 }
01088             }
01089 
01090         private:
01091 
01092             /**
01093              * Remove the private callback and call the onDisableAllPassword callback.
01094              * @param nfc Object triggering the command.
01095              * @param status Command status.
01096              */
01097             void onFinishCommand(M24SR *nfc,StatusTypeDef status) {
01098                 nfc->mComponentCallback=NULL;
01099                 mI2CPwd=NULL;
01100                 nfc->getCallback()->on_disable_all_password(nfc,status);
01101             }//onFinish
01102 
01103     };
01104 
01105 
01106     /**
01107      * Object containing the callback chain needed to remove the password request
01108      */
01109     RemoveAllPasswordCallback mRemoveAllPasswordCallback;
01110     friend class RemoveAllPasswordCallback;
01111 
01112     /**
01113      * This class permits to set the tag as read/write only
01114      * This class is equivalent to calling the methods:
01115      * <ul>
01116      *   <li> Verify(i2c) </li>
01117      *   <li> EnablePermanentState(Read/write) </li>
01118      * </ul>
01119      * or:
01120      * <ul>
01121      *   <li> Verify(i2c) </li>
01122      *   <li> DisablePermanentState</li>
01123      *   <li> disable_verification_requirement(Read/write) </li>
01124      * </ul>
01125      */
01126     class ChangeAccessStateCallback : public Callbacks{
01127 
01128         public:
01129 
01130             typedef enum{
01131                 WRITE,
01132                 READ
01133             }AccessType_t;
01134 
01135             /**
01136              * Build the chain of callbacks.
01137              */
01138             ChangeAccessStateCallback():mType(WRITE),mEnable(false) {}
01139 
01140             /**
01141              * Set the access to enable/disable an access type.
01142              * @param type Access type.
01143              * @param enable True to enable the state, False to disable it.
01144              */
01145             void change_access_state(AccessType_t type,bool enable) {
01146                 mType=type;
01147                 mEnable=enable;
01148             }
01149 
01150             virtual void on_verified(M24SR *nfc,StatusTypeDef status,
01151                     PasswordType_t,const uint8_t*) {
01152                 if (status!=M24SR_SUCCESS){
01153                     return onFinishCommand(nfc,status);
01154                 }
01155 
01156                 if (mEnable) {
01157                     nfc->disable_permanent_state(mType==WRITE? WritePwd : ReadPwd);
01158                 } else {
01159                     nfc->enable_permanent_state(mType==WRITE? WritePwd : ReadPwd);
01160                 }
01161 
01162             }
01163 
01164             virtual void on_disable_permanent_state(M24SR *nfc, StatusTypeDef status,
01165                     PasswordType_t type ) {
01166                 if (status!=M24SR_SUCCESS) {
01167                     return onFinishCommand(nfc,status);
01168                 }
01169 
01170                 nfc->disable_verification_requirement(type);
01171             }
01172 
01173             virtual void on_disable_verification_requirement(M24SR *nfc , StatusTypeDef status, PasswordType_t ) {
01174                 onFinishCommand(nfc,status);
01175             }
01176 
01177             virtual void on_enable_permanent_state(M24SR *nfc ,StatusTypeDef status,PasswordType_t ) {
01178                 onFinishCommand(nfc,status);
01179             }
01180 
01181 
01182         private:
01183 
01184             /**
01185              * Remove the private callback and call the user callback.
01186              * @param nfc Object triggering the command.
01187              * @param status Command status.
01188              */
01189             void onFinishCommand(M24SR *nfc,StatusTypeDef status) {
01190                 nfc->mComponentCallback=NULL;
01191                 if (mEnable) {
01192                     if (mType==READ) {
01193                         //enable read = disable write only
01194                         nfc->getCallback()->on_disable_write_only(nfc,status);
01195                     } else {
01196                         //enable write = disable read only
01197                         nfc->getCallback()->on_disable_read_only(nfc,status);
01198                     }
01199                 } else {
01200                     if (mType==WRITE) {
01201                         //disable write = enable read only
01202                         nfc->getCallback()->on_enable_read_only(nfc,status);
01203                     } else {
01204                         //
01205                         nfc->getCallback()->on_enable_write_only(nfc,status);
01206                     }
01207                 }//if-else enable
01208             }//onFinish
01209 
01210             AccessType_t mType;
01211             bool mEnable;
01212     };
01213 
01214 
01215     /**
01216      * Object containing the callback chain needed to change the access state
01217      */
01218     ChangeAccessStateCallback mChangeAccessStateCallback;
01219     friend class ChangeAccessStateCallback;
01220 
01221 
01222     /**
01223      * Object with the callback used to send a ManageGPO command.
01224      * This class is equivalent to calling the methods:
01225      * <ul>
01226      *   <li> SelectedApplication </li>
01227      *   <li> SelectSystemfile </li>
01228      *   <li> ReadBinar: read the old value </li>
01229      *   <li> onVerified </li>
01230      *   <li> onUpdatedBinary: write the new value</li>
01231      * </ul>
01232      */
01233     class ManageGPOCallback : public Callbacks{
01234 
01235     public:
01236 
01237         /**
01238          * Build the chain of callbacks.
01239          * @param parent Parent component to run the command on.
01240          */
01241         ManageGPOCallback(M24SR &parent):mParent(parent),
01242                 mGpoConfig(HIGH_IMPEDANCE),mReadGpoConfig(0),mI2CGpo(true) {}
01243 
01244 
01245         /**
01246          * Command parameters.
01247          * @param i2cGpo true to change the i2c gpo, false for the rf gpo.
01248          * @param newConfig new gpo function.
01249          */
01250         void set_new_GPO_config(bool i2cGpo,NFC_GPO_MGMT newConfig) {
01251             mGpoConfig=newConfig;
01252             mI2CGpo =i2cGpo;
01253         }
01254 
01255         virtual void on_selected_application(M24SR *nfc,StatusTypeDef status) {
01256             if (status==M24SR_SUCCESS) {
01257                 nfc->select_system_file();
01258             } else {
01259                 onFinishCommand(nfc,status);
01260             }
01261         }
01262 
01263         virtual void on_selected_system_file(M24SR *nfc,StatusTypeDef status) {
01264             if (status==M24SR_SUCCESS) {
01265                 nfc->read_binary(0x0004, 0x01, &mReadGpoConfig);
01266             } else {
01267                 onFinishCommand(nfc,status);
01268             }
01269         }
01270 
01271         virtual void on_read_byte(M24SR *nfc,StatusTypeDef status,
01272                 uint16_t,uint8_t*, uint16_t ) {
01273             if (status==M24SR_SUCCESS) {
01274                 nfc->verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD);
01275             } else {
01276                 onFinishCommand(nfc,status);
01277             }
01278         }
01279 
01280         virtual void on_verified(M24SR *nfc,StatusTypeDef status,PasswordType_t, const uint8_t*) {
01281             if (status!=M24SR_SUCCESS) {
01282                 return onFinishCommand(nfc,status);
01283             }
01284 
01285             if (mI2CGpo) {
01286                 mReadGpoConfig = (mReadGpoConfig & 0xF0) | (uint8_t)mGpoConfig;
01287             } else {
01288                 mReadGpoConfig = (mReadGpoConfig & 0x0F) | (((uint8_t)mGpoConfig)<<4);
01289             }
01290 
01291             nfc->update_binary(0x0004, 0x01, &mReadGpoConfig);
01292         }
01293 
01294         virtual void on_updated_binary(M24SR *nfc,StatusTypeDef status,
01295                 uint16_t , uint8_t*, uint16_t ) {
01296 
01297             if (status==M24SR_SUCCESS) {
01298                 if (mGpoConfig == I2C_ANSWER_READY) {
01299                     mParent.mCommunicationType = ASYNC;
01300                 } else {
01301                     mParent.mCommunicationType = SYNC;
01302                 }//if-else
01303             }//status
01304             onFinishCommand(nfc,status);
01305         }
01306 
01307     private:
01308 
01309         /**
01310          * Remove the private callback and call the user callback.
01311          * @param nfc Object where the command was send to.
01312          * @param status Command status.
01313          */
01314         void onFinishCommand(M24SR *nfc,StatusTypeDef status) {
01315             mParent.mComponentCallback=NULL;
01316             if (mI2CGpo) {
01317                 mParent.mCallback->on_manage_I2C_GPO(nfc,status,mGpoConfig);
01318             } else {
01319                 mParent.mCallback->on_manage_RF_GPO(nfc,status,mGpoConfig);
01320             }//if-else
01321         }//onFinishCommand
01322 
01323         /**
01324          * Component where send the commands
01325          */
01326         M24SR &mParent;
01327 
01328         /** new gpo function that this class has to write */
01329         NFC_GPO_MGMT mGpoConfig;
01330 
01331         /**
01332          * variable where storeing the read gpo configuration
01333          */
01334         uint8_t mReadGpoConfig;
01335 
01336         /**
01337          * true to change the i2c gpo, false to change the rf gpo
01338          */
01339         bool mI2CGpo;
01340 
01341     };
01342 
01343     /**
01344      * Object containing the callback chain needed to change the gpo function
01345      */
01346     ManageGPOCallback mManageGPOCallback;
01347 
01348     /**
01349      * Object with the callback used to read the component ID
01350      * This class is equivalent to calling the methods:
01351      * <ul>
01352      *   <li> SelectedApplication </li>
01353      *   <li> SelectSystemfile </li>
01354      *   <li> read_binary </li>
01355      * </ul>
01356      */
01357     class ReadIDCallback : public Callbacks {
01358 
01359         public:
01360 
01361             /**
01362              * Build the chain of callbacks.
01363              * @param parent object where to send the command to.
01364              */
01365             ReadIDCallback(M24SR &parent):mParent(parent),mIdPtr(NULL) {}
01366 
01367             /**
01368              * Set the variable containing the result
01369              * @param idPtr
01370              */
01371             void read_id_on(uint8_t *idPtr) {
01372                 mIdPtr = idPtr;
01373             }
01374 
01375             virtual void on_selected_application(M24SR *nfc,StatusTypeDef status) {
01376                 if (status==M24SR_SUCCESS) {
01377                     nfc->select_system_file();
01378                 } else {
01379                     onFinishCommand(nfc,status);
01380                 }
01381 
01382             }
01383 
01384             virtual void on_selected_system_file(M24SR *nfc,StatusTypeDef status) {
01385                 if (status==M24SR_SUCCESS) {
01386                     nfc->read_binary(0x0011, 0x01, mIdPtr);
01387                 } else {
01388                     onFinishCommand(nfc,status);
01389                 }
01390             }
01391 
01392             virtual void on_read_byte(M24SR *nfc,StatusTypeDef status,
01393                 uint16_t ,uint8_t *, uint16_t ) {
01394                 onFinishCommand(nfc,status);
01395             }
01396 
01397         private:
01398 
01399             /**
01400              * Remove the private callback and call the user onReadId function.
01401              * @param nfc Object where the command was send.
01402              * @param status Command status.
01403              */
01404             void onFinishCommand(M24SR *nfc,StatusTypeDef status) {
01405                 mParent.mComponentCallback=NULL;
01406                 mParent.getCallback()->on_read_id(nfc,status,mIdPtr);
01407             }
01408 
01409             M24SR& mParent;
01410 
01411             /**
01412              * pointer to read id
01413              */
01414             uint8_t *mIdPtr;
01415     };
01416 
01417 
01418     /**
01419      * Object containing the callback chain needed to read the component id
01420      */
01421     ReadIDCallback mReadIDCallback;
01422     friend class ReadIDCallback;
01423 
01424     /**
01425      * Convert a generic enum to the value used by the M24SR chip.
01426      * @param type Password type.
01427      * @return equivalent value used inside the m24sr chip */
01428     uint16_t passwordTypeToConst(const PasswordType_t &type) const {
01429         switch (type) {
01430             case ReadPwd:
01431                 return READ_PWD;
01432             case WritePwd:
01433                 return WRITE_PWD;
01434             case I2CPwd:
01435             default:
01436                 return I2C_PWD;
01437         }//switch
01438     }//passwordTypeToConst
01439     
01440     /**
01441      * convert  a uint16 into an enum value
01442      * @param type
01443      * @return
01444      */
01445     PasswordType_t constToPasswordType(const uint16_t type)const{
01446         switch(type) {
01447             case READ_PWD:
01448                 return ReadPwd;
01449             case WRITE_PWD:
01450                 return WritePwd;
01451             case I2C_PWD:
01452             default:
01453                 return I2CPwd;
01454         }//switch
01455     }//passwordTypeToConst
01456 };
01457 
01458 #endif // __M24SR_CLASS_H
01459 
01460 
01461 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/