Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Mime.h Source File

Mime.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef NFC_COMMON_MIME_H_
00018 #define NFC_COMMON_MIME_H_
00019 
00020 #include <stdint.h>
00021 
00022 #include "platform/Span.h"
00023 
00024 #include "nfc/ndef/RecordParser.h"
00025 #include "nfc/ndef/MessageBuilder.h"
00026 
00027 namespace mbed {
00028 namespace nfc {
00029 namespace ndef {
00030 namespace common {
00031 
00032 /**
00033  * @addtogroup nfc
00034  * @{
00035  */
00036 
00037 /**
00038  * Represent a mime object.
00039  */
00040 class Mime {
00041 public:
00042     /**
00043      * Construct an empty Mime object.
00044      */
00045     Mime();
00046 
00047     /**
00048      * Construct a mime object from its type and content
00049      *
00050      * @param mime_type The mime type of the object.
00051      * @param content The content of the object.
00052      *
00053      * @note To remove the NULL terminator of the C-string of the mime_type
00054      * parameter, you can use the utility function span_from_cstr.
00055      */
00056     Mime(
00057         const Span<const uint8_t>  &mime_type,
00058         const Span<const uint8_t>  &content
00059     );
00060 
00061     /**
00062      * Copy construct a Mime object.
00063      * @param other The Mime object copied.
00064      */
00065     Mime(const Mime &other);
00066 
00067     /**
00068      * Destroy a Mime object.
00069      */
00070     ~Mime();
00071 
00072     /**
00073      * Copy assign a Mime object.
00074      *
00075      * @param other The Mime object to copy.
00076      *
00077      * @return a reference to this object
00078      */
00079     Mime &operator=(const Mime &other);
00080 
00081     /**
00082      * Set all attributes of a mime object.
00083      *
00084      * @param mime_type Type of the mime object.
00085      * @param content Content of the mime object.
00086      *
00087      * @note To remove the NULL terminator of the C-string of the mime_type
00088      * parameter, you can use the utility function span_from_cstr.
00089      */
00090     void set_mime(
00091         const Span<const uint8_t>  &mime_type,
00092         const Span<const uint8_t>  &content
00093     );
00094 
00095     /**
00096      * Return the mime type.
00097      * @return The mime type.
00098      */
00099     Span<const uint8_t>  get_mime_type() const;
00100 
00101     /**
00102      * Return the content of the mime object.
00103      * @return the content of the mime object.
00104      */
00105     Span<const uint8_t>  get_mime_content() const;
00106 
00107     /**
00108      * Append into a message builder
00109      */
00110     bool append_as_record(
00111         MessageBuilder &message_builder,
00112         bool is_last_record = false
00113     ) const;
00114 
00115     /**
00116      * Compute the size of this Mime object in a ndef record.
00117      *
00118      * @return The size of the ndef record required to store this object.
00119      */
00120     size_t get_record_size() const;
00121 
00122 private:
00123     friend class MimeParser;
00124 
00125     void move_data(
00126         uint8_t *mime_record,
00127         size_t mime_type_size,
00128         size_t mime_content_size
00129     );
00130 
00131     size_t mime_size() const;
00132 
00133     uint8_t *_mime;
00134     size_t _type_size;
00135     size_t _content_size;
00136 };
00137 
00138 /**
00139  * Parse a Mime payload.
00140  */
00141 class MimeParser : public GenericRecordParser<MimeParser, Mime> {
00142 public:
00143     bool do_parse(const Record &record, Mime &mime);
00144 };
00145 
00146 /**
00147  * @}
00148  */
00149 
00150 } // namespace common
00151 } // namespace ndef
00152 } // namespace nfc
00153 } // namespace mbed
00154 
00155 #endif /* NFC_COMMON_MIME_H_ */