FreeRTOS
aws_greengrass_discovery.h
Go to the documentation of this file.
1 /*
2  * FreeRTOS
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://aws.amazon.com/freertos
23  * http://www.FreeRTOS.org
24  */
25 
31 #ifndef _AWS_GREENGRASS_DISCOVERY_H_
32 #define _AWS_GREENGRASS_DISCOVERY_H_
33 #include "FreeRTOS.h"
34 #include "aws_clientcredential.h"
35 #include "aws_secure_sockets.h"
36 
45 typedef struct
46 {
47  const char * pcGroupName;
48  const char * pcCoreAddress;
49  uint8_t ucInterface;
51 
59 typedef struct
60 {
61  const char * pcHostAddress;
62  char * pcCertificate;
63  uint32_t ulCertificateSize;
64  uint16_t usPort;
66 
67 /*
68  * @brief Connect directly to the green grass core.
69  *
70  * @note: In most case only calling this function is needed!
71  * This function will perform in series:
72  * 1. GGD_JSONRequest.
73  * 2. GGD_GetJSONFileSize.
74  * 3. GGD_GetJSONFile.
75  * 4. GGD_ConnectToHost with auto slection parameters set to true.
76  * The buffer size of pcBuffer need to be big enough to hold the complete
77  * JSON file.
78  *
79  * @param [in] pcBuffer: Memory buffer provided by the user.
80  *
81  * @param [in] ulBufferSize: Size of the memory buffer.
82  *
83  * @param [out] pxHostAddressData : host address data
84  *
85  * @return If connection was successful then pdPASS is
86  * returned. Otherwise pdFAIL is returned.
87  */
88 BaseType_t GGD_GetGGCIPandCertificate( char * pcBuffer,
89  const uint32_t ulBufferSize,
90  GGD_HostAddressData_t * pxHostAddressData );
91 
92 /*
93  * @brief HTML request to get the JSON file from the could.
94  *
95  * @note: This call will open a socket. Socket will need to be closed
96  * by either calling GGD_JSONRequestGetFile or GGD_JSONRequestAbort
97  *
98  * @param [out] pxSocket: Socket for the cloud connection.
99  *
100  * @return If the JSON request was performed successfully
101  * then pdPASS is returned. Otherwise pdFAIL is returned.
102  */
103 BaseType_t GGD_JSONRequestStart( Socket_t * pxSocket );
104 
105 /*
106  * @brief Get the size of the requested JSON file.
107  *
108  * @note: Parse the HTML header to get size of the requested
109  * JSON file.
110  * The JSON file request through "GGD_JSONRequestStart"
111  * has to be done prior to call this function.
112  *
113  * @param [in] pxSocket: Socket for the cloud connection.
114  * Returns SOCKETS_INVALID_SOCKET if connection is closed
115  *
116  * @param [out] pulJSONFileSize: The size of the requested JSON file.
117  *
118  * @return If the JSON file size was received successfully
119  * then pdPASS is returned. Otherwise pdFAIL is returned.
120  */
121 BaseType_t GGD_JSONRequestGetSize( Socket_t * pxSocket,
122  uint32_t * pulJSONFileSize );
123 
124 /*
125  * @brief Get the GreenGrass core JSON file from the cloud.
126  *
127  * This call will close the socket in parameter. Need previous call from
128  * GGD_JSONRequestStart.
129  *
130  * @note 1. Parse the HTML response to extract the JSON file.
131  * This function allows the JSON file retrieval in chunck.
132  * The user can call the function several time with small buffer size.
133  * The JSON file will be retrieved bit by bit until
134  * xJSONFileRetrieveCompleted is set to true.
135  * 2. The JSON file request through "GGD_JSONRequestStart"
136  * has to be done prior to call this function.
137  * 3. The JSON file size has to be provided so calling
138  * GGD_JSONRequestGetSize prior is mandatory.
139  *
140  * @param [in] pxSocket: Socket for the cloud connection.
141  * @warning The socket Will be closed.Set to SOCKETS_INVALID_SOCKET.
142  *
143  * @param [in] pcBuffer: Memory buffer provided by the user.
144  *
145  * @param [in] ulBufferSize: Size of the memory buffer provided.
146  *
147  * @param [in] pulJSONFileSize: Size of JSON file to be retrieved.
148  *
149  * @param [out] pulByteRead: Must be set to zero by the user
150  * prior first calling of GGD_GetJSONFile. Then the number of
151  * bytes read from the server will be update with each GGD_GetJSONFile
152  * call.
153  *
154  * @param [out] pxJSONFileRetrieveCompleted: set to pdTRUE when
155  * JSON file retrieve is finish.
156  *
157  * @return If the request check was sucessfully retrieved.
158  * Otherwise pdFAIL is returned.
159  * @note The condition for complete JSON retrieval is
160  * xJSONFileRetrieveCompleted == pdTRUE and
161  * function return pdPASS for every call
162  */
163 BaseType_t GGD_JSONRequestGetFile( Socket_t * pxSocket,
164  char * pcBuffer,
165  const uint32_t ulBufferSize,
166  uint32_t * pulByteRead,
167  BaseType_t * pxJSONFileRetrieveCompleted,
168  const uint32_t pulJSONFileSize );
169 
170 /*
171  * @brief Need to be called if GGD_JSONRequestGetFile cannot be called.
172  *
173  * @param [in] pxSocket: Socket to close the cloud connection.Set to SOCKETS_INVALID_SOCKET.
174  *
175  */
176 void GGD_JSONRequestAbort( Socket_t * pxSocket );
177 
178 /*
179  * @brief Get host IP and certificate
180  *
181  * Get host IP and certificate with the JSON file given the greengrass group
182  * and cloud core address the user wants to connect.
183  *
184  * @note: The JSON file contains the certificate that is going to be used to
185  * connect with the core. For memory savings, the JSON file certificate is
186  * going to be editing inline to connect with the host.
187  * Thus, afterwards, the JSON will become unusable.
188  * This function will also change the default certificate being used.
189  * The default certificate is the one specified in client credential.
190  * After calling this function, it will be one of the certificates provided in the
191  * JSON file.
192  * @note that if xAutoSelectFlag is set to false, the user will have to provide the
193  * pxHostParameters. If xAutoSelectFlag is set to true, then pxHostParameters
194  * are not used (can be set to NULL) and the IP and certificate returned will be of
195  * the first established connection.
196  * The user would also need to specify the ggdconfigCORE_NETWORK_INTERFACE (starting from 1)
197  * in aws_ggd_config.h
198  *
199  * @param [out] pxHostAddressData : host address data
200  *
201  * @param [in] pucJSON_File: Pointer to the JSON file. WARNING, will be modified to
202  * format the certificate.
203  *
204  * @param [in] ulJSON_File_Size: Size in byte of the array.
205  *
206  * @param [in] pxHostParameters: Contains the group name, cloud address of the desired
207  * core to connect to and interface to use (WIFI, ETH0 etc...).
208  * @warning: Cannot be NULL if xAutoSelectFlag is set to pdFALSE
209  *
210  * @param [in] xAutoSelectFlag: The user can opt for the auto select option.
211  * Then pxHostParameters are not used. Can be set to NULL.
212  *
213  * @return successfull pdPASS is
214  * returned. Otherwise pdFAIL is returned.
215  */
216 BaseType_t GGD_GetIPandCertificateFromJSON( char * pcJSONFile,
217  const uint32_t ulJSONFileSize,
218  const HostParameters_t * pxHostParameters,
219  GGD_HostAddressData_t * pxHostAddressData,
220  const BaseType_t xAutoSelectFlag );
221 #endif /* _AWS_GREENGRASS_DISCOVERY_H_ */
Secure Sockets Interface.
Green Grass Core connection parameters.
Input from user to locate GGC inside JSON file.
void * Socket_t
The socket type.