my customized lib
esp8266-driver/ESP8266/ATParser/BufferedSerial/BufferedPrint.c@0:8094b249013c, 2017-11-26 (annotated)
- Committer:
- DuyLionTran
- Date:
- Sun Nov 26 15:08:14 2017 +0000
- Revision:
- 0:8094b249013c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuyLionTran | 0:8094b249013c | 1 | /* |
DuyLionTran | 0:8094b249013c | 2 | * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
DuyLionTran | 0:8094b249013c | 3 | * SPDX-License-Identifier: Apache-2.0 |
DuyLionTran | 0:8094b249013c | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
DuyLionTran | 0:8094b249013c | 5 | * not use this file except in compliance with the License. |
DuyLionTran | 0:8094b249013c | 6 | * You may obtain a copy of the License at |
DuyLionTran | 0:8094b249013c | 7 | * |
DuyLionTran | 0:8094b249013c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
DuyLionTran | 0:8094b249013c | 9 | * |
DuyLionTran | 0:8094b249013c | 10 | * Unless required by applicable law or agreed to in writing, software |
DuyLionTran | 0:8094b249013c | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
DuyLionTran | 0:8094b249013c | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
DuyLionTran | 0:8094b249013c | 13 | * See the License for the specific language governing permissions and |
DuyLionTran | 0:8094b249013c | 14 | * limitations under the License. |
DuyLionTran | 0:8094b249013c | 15 | */ |
DuyLionTran | 0:8094b249013c | 16 | |
DuyLionTran | 0:8094b249013c | 17 | #include <stdarg.h> |
DuyLionTran | 0:8094b249013c | 18 | #include <stdint.h> |
DuyLionTran | 0:8094b249013c | 19 | #include <stdio.h> |
DuyLionTran | 0:8094b249013c | 20 | #include <string.h> |
DuyLionTran | 0:8094b249013c | 21 | |
DuyLionTran | 0:8094b249013c | 22 | #include "mbed_error.h" |
DuyLionTran | 0:8094b249013c | 23 | |
DuyLionTran | 0:8094b249013c | 24 | size_t BufferedSerialThunk(void *buf_serial, const void *s, size_t length); |
DuyLionTran | 0:8094b249013c | 25 | |
DuyLionTran | 0:8094b249013c | 26 | int BufferedPrintfC(void *stream, int size, const char* format, va_list arg) |
DuyLionTran | 0:8094b249013c | 27 | { |
DuyLionTran | 0:8094b249013c | 28 | int r; |
DuyLionTran | 0:8094b249013c | 29 | char buffer[512]; |
DuyLionTran | 0:8094b249013c | 30 | if (size >= 512) { |
DuyLionTran | 0:8094b249013c | 31 | return -1; |
DuyLionTran | 0:8094b249013c | 32 | } |
DuyLionTran | 0:8094b249013c | 33 | memset(buffer, 0, size); |
DuyLionTran | 0:8094b249013c | 34 | r = vsprintf(buffer, format, arg); |
DuyLionTran | 0:8094b249013c | 35 | // this may not hit the heap but should alert the user anyways |
DuyLionTran | 0:8094b249013c | 36 | if(r > (int32_t) size) { |
DuyLionTran | 0:8094b249013c | 37 | error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__, size, r); |
DuyLionTran | 0:8094b249013c | 38 | return 0; |
DuyLionTran | 0:8094b249013c | 39 | } |
DuyLionTran | 0:8094b249013c | 40 | if ( r > 0 ) { |
DuyLionTran | 0:8094b249013c | 41 | BufferedSerialThunk(stream, buffer, r); |
DuyLionTran | 0:8094b249013c | 42 | } |
DuyLionTran | 0:8094b249013c | 43 | return r; |
DuyLionTran | 0:8094b249013c | 44 | } |