1
+ #ifndef SparkFun_iAP2_H
2
+ #define SparkFun_iAP2_H
3
+
4
+ #include < Arduino.h>
5
+ #include < SparkFun_Auth_3_CP.h>
6
+
7
+ class SparkFuniAPDriverDebugPrint
8
+ {
9
+ public:
10
+ SparkFuniAPDriverDebugPrint ();
11
+ ~SparkFuniAPDriverDebugPrint ();
12
+
13
+ void enableDebug (Print *debugPrint);
14
+ void disableDebug (void );
15
+
16
+ void systemWrite (const char *string);
17
+ void systemPrintf (const char *format, ...);
18
+ void systemPrint (const char *string);
19
+ void systemPrintln (const char *string);
20
+ void systemPrintln ();
21
+
22
+ Print *_debugPrint = nullptr ;
23
+ };
24
+
25
+ class SparkFuniAP2Driver
26
+ {
27
+ public:
28
+ SparkFuniAP2Driver ();
29
+ ~SparkFuniAP2Driver ();
30
+ void begin ();
31
+ void enableDebug (Print *debugPrint);
32
+ void disableDebug (void );
33
+ void iap2ParseUpdate ();
34
+ void iap2Handler ();
35
+ void iap2LinkUpdate ();
36
+ void iap2TrafficUpdate ();
37
+ void startHandshake (Stream *theStream);
38
+ void setNMEApointers (char *latestGPGGA, char *latestGPRMC, char *latestGPGST = nullptr );
39
+ void setAuthPointers (uint8_t *authCert, size_t certSize, char *authCertSerial);
40
+ void setAuthCoprocessorPointer (SparkFunAuth3CPArdI2C *authCoprocessor);
41
+ void setAccessoryName (const char *accessoryName);
42
+ void setModelIdentifier (const char *modelIdentifier);
43
+ void setManufacturer (const char *manufacturer);
44
+ void setSerialNumber (const char *serialNumber);
45
+ void setFirmwareVersion (const char *firmwareVersion);
46
+ void setHardwareVersion (const char *hardwareVersion);
47
+ void setExternalAccessoryProtocol (const char *EAProtocol);
48
+ void setBluetoothTransportName (const char *BTTransportName);
49
+ void setLocationInfoComponentName (const char *LIComponentName);
50
+ void setProductPlanUID (const char *productPlanUID);
51
+ void setBluetoothMacAddress (const uint8_t *macAddress);
52
+ void setTransportConnectedMethod (void (*methodPtr)(bool *));
53
+ void setTransportDisconnectMethod (void (*methodPtr)(bool *));
54
+ void usePSRAM (bool enable); // Must be called before begin
55
+
56
+ private:
57
+ void iap2LinkSetState (uint8_t newState);
58
+ void iap2ParseSetState (uint8_t newState);
59
+ uint8_t iap2ParseGetState ();
60
+ void iap2TrafficSetState (uint8_t newState);
61
+ void iap2CreateSynPacket ();
62
+ void iap2SendAckPacket (uint8_t seqNumber, uint8_t ackNumber);
63
+ void sendSessionMessageBlobSingleParameter (uint16_t messageId, uint8_t iap2SessionId, uint8_t * parameterData, uint16_t parameterDataLength);
64
+ void sendSessionMessageBlobMultiParameter (uint16_t messageId, uint8_t iap2SessionId, uint8_t * parameterData, uint16_t parameterDataLength);
65
+ void sendExternalAccessorySessionData (uint16_t iap2SessionId, uint8_t * sessionData, uint16_t sessionDataLength);
66
+ void iap2CreatePacket (uint8_t *packet, uint8_t controlByte, uint8_t seqNumber, uint8_t ackNumber, uint8_t sessionID, uint8_t *data, uint16_t dataLength);
67
+ void iap2SendPacket (uint8_t *packet);
68
+ uint8_t iap2Checksum (uint8_t *data, uint16_t dataLength);
69
+ void iap2PrintPacket (uint8_t *packetData);
70
+ void iap2PrintRawPacket (uint8_t *packetData);
71
+ uint16_t iap2GetPacketLength (uint8_t *packet);
72
+ uint16_t iap2GetDataLength (uint8_t *packet);
73
+ int16_t parseSessionMessage (uint8_t *messageData, uint16_t *messageId);
74
+ bool parseSessionMessageParameter (uint8_t *messageData, uint16_t desiredParameter, uint8_t **parameterStart, uint16_t *parameterLength, uint16_t *parameterId);
75
+ bool addIdentificationInformation (const char *txt, uint8_t **information, uint16_t *informationLength, uint16_t id);
76
+ bool addIdentificationInformationUint8 (uint8_t val, uint8_t **information, uint16_t *informationLength, uint16_t id);
77
+ bool addIdentificationInformationUint16 (uint16_t val, uint8_t **information, uint16_t *informationLength, uint16_t id);
78
+ bool addIdentificationInformationNone (uint8_t **information, uint16_t *informationLength, uint16_t id);
79
+ bool addIdentificationInformationExternalAccessoryProtocol (uint8_t protocolIdentifier, const char *protocolName, uint8_t matchAction, uint16_t *transportIdentifier, uint8_t **information, uint16_t *informationLength, uint16_t id);
80
+ bool addIdentificationInformationBluetoothTransport (uint16_t transportIdentifier, const char *transportName, bool supportsiAP2, const uint8_t *macAddr, uint8_t **information, uint16_t *informationLength, uint16_t id);
81
+ bool addIdentificationInformationLocationInformation (uint16_t identifier, const char *Name, bool GPGGA, bool GPRMC, bool PASCD, uint8_t **information, uint16_t *informationLength, uint16_t id);
82
+ bool addIdentificationInformationMessagesSentReceived (uint16_t numMessagesReceived, uint16_t *messagesReceived, uint8_t **information, uint16_t *informationLength, uint16_t id);
83
+ bool addLocationInformation (const char *txt, uint8_t **information, uint16_t *informationLength, uint16_t id);
84
+ void printHexThing (const char *name, size_t thingSize, uint8_t *thing);
85
+
86
+ #define IAP2_MAX_PACKET_SIZE 2048 // Default for Bluetooth
87
+
88
+ unsigned long handshakeTimer = millis();
89
+ bool handshakeReceived = false ;
90
+
91
+ // iAP2 Packet Variables
92
+ uint8_t iap2Packet[IAP2_MAX_PACKET_SIZE];
93
+
94
+ uint16_t iap2ResponseSpot = 0 ;
95
+ uint16_t iap2ResponseLength = 0 ;
96
+ bool iap2ResponseReceived = false ; // Set true when a valid iAP2 packet is received
97
+
98
+ uint8_t iap2PacketSequenceNumber = 0 ; // Increment our PSN once the device ACK's it
99
+ uint8_t iap2DevicePacketSequenceNumber = 0 ; // We must keep track of the device's packet numbers to send ACK's
100
+
101
+ // Link Synchronization Payload variables
102
+ uint8_t iap2ControlSessionId = 0x0A ;
103
+ uint8_t iap2ExternalAccessorySessionId = 0x0B ;
104
+
105
+ bool iap2PacketDelivered = false ; // Goes true once a packet we send is ACK'd by the device
106
+ uint16_t iap2PacketSendTime; // Monitors ACK timeout
107
+
108
+ // Session Message Variables
109
+ uint8_t *iap2SessionMessage = nullptr ;
110
+
111
+ // Location Information
112
+ bool locationInformationStarted = false ;
113
+ bool gpggaRequested = false ; // Applies to Location Information only - not EA
114
+ bool gprmcRequested = false ; // Applies to Location Information only - not EA
115
+
116
+ // External Accessory
117
+ bool eaSessionStarted = false ;
118
+ uint32_t lastSend = 0 ;
119
+ uint16_t externalAccessoryProtocolSessionId = 0 ; // ID given to us to start an EA session
120
+
121
+ SparkFuniAPDriverDebugPrint _debugPrint;
122
+
123
+ Stream *_theStream; // Pointer to the transport stream
124
+
125
+ char *_latestGPGGA = nullptr ; // Pointer to the latest NMEA GGA, provided by an external GNSS
126
+ char *_latestGPRMC = nullptr ; // Pointer to the latest NMEA RMC, provided by an external GNSS
127
+ char *_latestGPGST = nullptr ; // Pointer to the latest NMEA GST, provided by an external GNSS
128
+
129
+ uint8_t *_authCert = nullptr ; // Storage for the authentication certificate
130
+ size_t _certSize = 0 ; // The size of the authentication certificate
131
+ char *_authCertSerial = nullptr ; // Storage for the certificate serial number
132
+
133
+ SparkFunAuth3CPArdI2C *_authCoprocessor = nullptr ; // Pointer to the instance of the authentication coprocessor
134
+
135
+ const char *_accessoryName = nullptr ;
136
+ const char *_modelIdentifier = nullptr ;
137
+ const char *_manufacturer = nullptr ;
138
+ const char *_serialNumber = nullptr ;
139
+ const char *_firmwareVersion = nullptr ;
140
+ const char *_hardwareVersion = nullptr ;
141
+ const char *_EAProtocol = nullptr ;
142
+ const char *_BTTransportName = nullptr ;
143
+ const char *_LIComponentName = nullptr ;
144
+ const char *_productPlanUID = nullptr ;
145
+ const uint8_t *_bluetoothMacAddress = nullptr ;
146
+ void (*_transportIsConnected)(bool *);
147
+ bool _transportIsConnectedDefined = false ;
148
+ void (*_transportDisconnect)(bool *);
149
+ bool _transportDisconnectDefined = false ;
150
+ bool _usePSRAM = false ;
151
+ };
152
+
153
+ #endif // #ifndef SparkFun_iAP2_H
0 commit comments