Ejemplo n.º 1
0
/**
 *  Convert a IPv4 address to a string
 *
 *  @note: String is 16 bytes including terminating null
 *
 * @param[out] buffer       : Buffer which will recieve the IPv4 string
 * @param[in]  ipv4_address : IPv4 address to convert
 */
static void ipv4_to_string( char buffer[16], uint32_t ipv4_address )
{
    uint8_t* ip = (uint8_t*)&ipv4_address;
    unsigned_to_decimal_string(ip[0], &buffer[0], 3, 3);
    buffer[3] = '.';
    unsigned_to_decimal_string(ip[1], &buffer[4], 3, 3);
    buffer[7] = '.';
    unsigned_to_decimal_string(ip[2], &buffer[8], 3, 3);
    buffer[11] = '.';
    unsigned_to_decimal_string(ip[3], &buffer[12], 3, 3);
}
Ejemplo n.º 2
0
wiced_result_t wiced_time_get_iso8601_time(wiced_iso8601_time_t* iso8601_time)
{
    wiced_utc_time_ms_t utc_time_ms;
    uint32_t            a;
    uint16_t            year;
    uint8_t             number_of_leap_years;
    uint8_t             month;
    uint8_t             day;
    uint8_t             hour;
    uint8_t             minute;
    uint64_t            second;
    uint16_t            sub_second;
    wiced_bool_t        is_a_leap_year;

    wiced_time_get_utc_time_ms( &utc_time_ms );

    second     = utc_time_ms / 1000;               /* Time is in milliseconds. Convert to seconds */
    sub_second = (uint16_t) (( utc_time_ms % 1000 ) * 1000 ); /* Sub-second is in microseconds               */

    /* Calculate year */
    year = (uint16_t)( 1970 + second / SECONDS_IN_365_DAY_YEAR );
    number_of_leap_years = (uint8_t) (( year - 1968 - 1 ) / 4 );
    second -= (uint64_t)( (uint64_t)( year - 1970 ) * SECONDS_IN_365_DAY_YEAR );
    if ( second > ( number_of_leap_years * SECONDS_IN_A_DAY ) )
    {
        second -= (uint64_t) ( ( number_of_leap_years * SECONDS_IN_A_DAY ) );
    }
    else
    {
        second -= (uint64_t) ( ( number_of_leap_years * SECONDS_IN_A_DAY ) + SECONDS_IN_365_DAY_YEAR );
        --year;
    }

    /* Remember if the current year is a leap year */
    is_a_leap_year = ( ( year - 1968 ) % 4 == 0 ) ? WICED_TRUE : WICED_FALSE;

    /* Calculate month */
    month = 1;

    for ( a = 0; a < 12; ++a )
    {
        uint32_t seconds_per_month = secondsPerMonth[a];
        /* Compensate for leap year */
        if ( ( a == 1 ) && is_a_leap_year )
        {
            seconds_per_month += SECONDS_IN_A_DAY;
        }
        if ( second > seconds_per_month )
        {
            second -= seconds_per_month;
            month++;
        }
        else
        {
            break;
        }
    }

    /* Calculate day */
    day    = (uint8_t) ( second / SECONDS_IN_A_DAY );
    second -= (uint64_t) ( day * SECONDS_IN_A_DAY );
    ++day;

    /* Calculate hour */
    hour   = (uint8_t) ( second / SECONDS_IN_A_HOUR );
    second -= (uint64_t)  ( hour * SECONDS_IN_A_HOUR );

    /* Calculate minute */
    minute = (uint8_t) ( second / SECONDS_IN_A_MINUTE );
    second -= (uint64_t) ( minute * SECONDS_IN_A_MINUTE );

    /* Write iso8601 time (Note terminating nulls get overwritten intentionally) */
    unsigned_to_decimal_string( year,             iso8601_time->year,       4, 4 );
    unsigned_to_decimal_string( month,            iso8601_time->month,      2, 2 );
    unsigned_to_decimal_string( day,              iso8601_time->day,        2, 2 );
    unsigned_to_decimal_string( hour,             iso8601_time->hour,       2, 2 );
    unsigned_to_decimal_string( minute,           iso8601_time->minute,     2, 2 );
    unsigned_to_decimal_string( (uint8_t)second,  iso8601_time->second,     2, 2 );
    unsigned_to_decimal_string( sub_second,       iso8601_time->sub_second, 6, 6 );

    iso8601_time->T          = 'T';
    iso8601_time->Z          = 'Z';
    iso8601_time->colon1     = ':';
    iso8601_time->colon2     = ':';
    iso8601_time->dash1      = '-';
    iso8601_time->dash2      = '-';
    iso8601_time->decimal    = '.';

    return WICED_SUCCESS;
}