|
AWS IoT Device SDK C:
Main
|
| Return to main page ↑ |
Guide for the coding style used in this SDK.
The goal of this style guide is to encourage a readable and consistent coding style across the entire SDK.
The coding style used in this SDK.
The coding style aims to produce code that is readable and easy to debug. An example is provided in Example File.
General guidelines for library style.
/* and */ should be used to start and end comments.goto is discouraged.iot_config.h at the top of the file before any other includes.static functions must have a declaration at the top of the file and be implemented before any application-facing functions.Guidelines for variable types.
bool and types required by third-party APIs.int32_t or uint32_t.size_t, and Boolean variables with bool.An example file that follows the coding style rules.
See Naming for how to name the functions, variables, and macros.
Naming convention used in this SDK.
The naming convention aims to differentiate this SDK's files, variables, and functions to avoid name collisions. In general:
iot_ or Iot. aws_iot_ or AwsIot.iot_mqtt_api.c identifies a file as part of the general MQTT library. _IotMqtt_ValidateConnect identifies a function as part of the Internal component of the general MQTT library.Naming convention for constants set using preprocessor # and enum values.define
This table contains the formats for the names of the most common SDK defined constants and enum values. It is not intended to be comprehensive. Bold text indicates a variable but required part of the defined constants and enum values name. Italic text indicates a variable but optional part of the defined constants and enum values name.
| Format | Applies to | Example |
|---|---|---|
IOT_LIBRARY_DESCRIPTIONAWS_IOT_LIBRARY_DESCRIPTION | Defined constants and enum values in application-facing library header files | IOT_MQTT_SUCCESS (iot_mqtt.h)AWS_IOT_SHADOW_SUCCESS (aws_iot_shadow.h) |
IOT_DEMO_DESCRIPTIONIOT_TEST_DESCRIPTION | Defined constants and enum values in demos and tests | IOT_DEMO_MQTT_PUBLISH_BURST_SIZEIOT_TEST_MQTT_THREADS |
AWS_IOT_DEMO_DESCRIPTIONAWS_IOT_TEST_DESCRIPTION | Defined constants and enum values in demos and tests specific to AWS IoT | AWS_IOT_DEMO_THING_NAMEAWS_IOT_TEST_SHADOW_THING_NAME |
| DESCRIPTION | Internal constants and enum values No AWS_ prefix for internal names in AWS-specific libraries | MQTT_PACKET_TYPE_CONNECT(iot_mqtt_internal.h) SHADOW_OPERATION_COUNT(aws_iot_shadow_internal.h) |
Names of constants and enum values should contain only uppercase letters and underscores. All names intended for application use must begin with IOT_ or AWS_IOT_.
Naming convention for files.
This table contains the formats for the names of the most common SDK files. It is not intended to be comprehensive. Bold text indicates a variable but required part of the files name. Italic text indicates a variable but optional part of the files name.
| Format | Applies to | Example |
|---|---|---|
iot_library_description.extensionaws_iot_library_description.extension | General library file | iot_mqtt_api.caws_iot_shadow_api.c |
iot_library_internal.haws_iot_library_internal.h | Internal library header | iot_mqtt_internal.haws_iot_shadow_internal.h |
iot_demo_library.caws_iot_demo_library.c | Library demo source | iot_demo_mqtt.caws_iot_demo_shadow.c |
iot_tests_library_description.caws_iot_tests_library_description.c | Library test source | iot_tests_mqtt_api.caws_iot_tests_shadow_api.c |
File names contain only lowercase letters and underscores. All file names should start with iot_ or aws_iot_ and be named according to their purpose. For example:
iot_mqtt_api.c: A file in the MQTT library that implements the MQTT API functions.iot_demo_mqtt.c: A file in the Demos for the MQTT library.iot_demo_mqtt_posix.c: A file in the Demos for the MQTT library on POSIX systems.iot_tests_mqtt_api.c: A file in the Tests for the MQTT library. Since the tests currently only run on POSIX systems, test file names do not use the _posix suffix.Library file names should use one or two words to describe the functions implemented in that file. For example:
iot_mqtt_serialize.c: Implements the MQTT library's packet serialization and deserialization functions.iot_clock_posix.c: Implements the platform clock component for POSIX systems.Declarations of internal functions, structures, macros, etc. of a library should be placed in a header file with an _internal suffix. The _internal header file should go in the lib/include/private directory. For example:
iot_mqtt_internal.h: Declares the MQTT library's internal functions, structures, macros, etc.File names for tests and demos should all begin with iot_demo_ and iot_tests_, respectively. The names should then specify the library being demoed or or tested; for example, the files names of the MQTT library's demos and tests start with iot_demo_mqtt_ and iot_tests_mqtt_. Additionally, test file names should describe what tests are implemented in the file, such as iot_tests_mqtt_api.c for a file containing tests for the MQTT library API functions.
Naming convention of functions and function-like macros.
This table contains the formats for the names of the most common SDK functions. It is not intended to be comprehensive. Bold text indicates a variable but required part of the functions name. Italic text indicates a variable but optional part of the functions name.
| Format | Applies to | Example |
|---|---|---|
IotLibrary_DescriptionAwsIotLibrary_Description | Externally-visible library function | IotMqtt_PublishAwsIotShadow_Update |
_IotLibrary_Description_AwsIotLibrary_Description | Internal (but not static) library function | _IotMqtt_ValidateNetIf_AwsIotShadow_ParseShadowStatus |
IotTestLibrary_Description | Function only used in tests | IotTest_NetworkConnectIotTestMqtt_createMqttConnection |
_description | static function (never uses Aws prefix) | _createMqttConnection_codeToShadowStatus |
IotTestLibrary_accessedFunction | Test access function | IotTestMqtt_createMqttConnection |
Externally visible (i.e. not static) functions are UpperCamelCased and must begin with Iot or AwsIot (public API functions); or _Iot or _AwsIot (internal functions). Function names should then specify their library name; followed by an underscore; followed by a brief description of what the function does. For example:
IotMqtt_Publish: This function is part of the public MQTT API. It Publishes an MQTT message._IotMqtt_ValidateNetIf: This function is internal to the MQTT library, but not static. It validates an IotMqttNetIf_t.Functions not visible outside their source file (i.e. static functions) have names that are lowerCamelCased and begin with an underscore. These function names do not contain the library name or Aws. For example:
_createMqttConnection: A static function in iot_mqtt_api.c._codeToShadowStatus: A static function in aws_iot_shadow_parser.c.Test access functions begin with IotTest; followed by the library name; followed by an underscore; followed by the function that the test function accesses. Since the accessed function is always static, the accessed function will be lowerCamelCased.
Naming conventions of library typedef types.
This table contains the formats for the names of the most common SDK types. It is not intended to be comprehensive. Bold text indicates a variable but required part of the types name. Italic text indicates a variable but optional part of the types name.
| Format | Applies to | Example |
|---|---|---|
IotLibraryDescription_tAwsIotLibraryDescription_t | General types in application-facing library header files | IotMqttError_t (iot_mqtt.h)AwsIotShadowError_t (aws_iot_shadow.h) |
IotLibraryFunctionInfo_tAwsIotLibraryFunctionInfo_t | Application-facing parameter structure | IotMqttPublishInfo_t(Parameter structure to IotMqtt_Publish)AwsIotShadowDocumentInfo_t (Parameter structure for Shadow documents) |
_libraryDescription_t | Type in an internal header | _mqttOperation_t (iot_mqtt_internal.h)_shadowOperation_t (aws_iot_shadow_internal.h) |
_description_t | Internal type in source file | _topicMatchParams_t (iot_mqtt_subscription.c) |
Types intended for use in applications are UpperCamelCased. The names must start with Iot or AwsIot and end with _t. Parameter structures should indicate their associated function: for example, IotMqttPublishInfo_t is passed as a parameter to IotMqtt_Publish.
Types intended for internal library use defined in a header file are lowerCamelCased. The names must start with _, followed by the library name, and end with _t. Internal types defined in a library source file must start with _, end with _t, and not include the library name.
struct typedefs should always be named along with the typedef. The struct name should be identical to the typedef name, but without the _t at the end. For example:
A struct may contain anonymous struct or union members.
Naming conventions of variables.
This table contains the formats for the names of the most common SDK variables. It is not intended to be comprehensive. Bold text indicates a variable but required part of the variables name. Italic text indicates a variable but optional part of the variables name.
| Format | Applies to | Example |
|---|---|---|
| variableDescription | General local variable | startTime |
pVariableDescription | Local variable pointers and arrays (including strings) | pSubscriptionList |
_variableDescription | Global variable that is static | _connectMutex (iot_mqtt_api.c)_pSamplePayload (string) |
_IotLibraryDescription_AwsIotLibraryDescription | Global variable that is NOT static | _IotMqttTaskPool (iot_mqtt_operation.c)_pIotSamplePayload (string) |
Local variable names are lowerCamelCased and consist only of a description of the variable. Names like i or j are acceptable for loop counters, but all other variables should have a descriptive name.
Global variable names always start with a _. Global variables that are static consist of only the description in lowerCamelCase. Global variables that are not static consist of the library name and the description in UpperCamelCase; for example: _IotLibraryNonStaticGlobalVariable.
All pointers, arrays, and strings (both global and local) start with p (local) or _p (global).