takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileHandle_stub.h Source File

FileHandle_stub.h

00001 /*
00002  * Copyright (c) , Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef __FILE_HANDLE_STUB_H__
00019 #define __FILE_HANDLE_STUB_H__
00020 
00021 #include "FileHandle.h"
00022 
00023 namespace mbed
00024 {
00025 
00026 static uint8_t filehandle_stub_short_value_counter = 0;
00027 static char *filehandle_stub_table = NULL;
00028 static uint8_t filehandle_stub_table_pos = 0;
00029 
00030 class FileHandle_stub : public FileHandle
00031 {
00032   public:
00033     ssize_t size_value;
00034 
00035     FileHandle_stub() { size_value = 0; }
00036 
00037     virtual ssize_t read(void *buffer, size_t size)
00038     {
00039         if (filehandle_stub_table)
00040         {
00041             ssize_t ret = strlen(filehandle_stub_table) - filehandle_stub_table_pos;
00042             if (size < ret)
00043             {
00044                 ret = size;
00045             }
00046             memcpy(buffer, filehandle_stub_table, ret);
00047             filehandle_stub_table_pos += ret;
00048             return ret;
00049         }
00050         return 0;
00051     }
00052 
00053     virtual ssize_t write(const void *buffer, size_t size)
00054     {
00055         if (size_value > 0)
00056         {
00057             size_value--;
00058             return size;
00059         }
00060         else if (size_value < 0)
00061         {
00062             return -1;
00063         }
00064         return 0;
00065     }
00066 
00067     virtual off_t seek(off_t offset, int whence = SEEK_SET) { return 0; }
00068 
00069     virtual int close() {}
00070 
00071     virtual short poll(short events) const
00072     {
00073         if (filehandle_stub_short_value_counter)
00074         {
00075             filehandle_stub_short_value_counter--;
00076             return short_value;
00077         }
00078         return 0;
00079     }
00080 
00081     virtual void sigio(Callback<void()> func) { func(); }
00082 
00083     short short_value;
00084 };
00085 
00086 } // namespace mbed
00087 
00088 #endif