[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-09 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r232236907
 
 

 ##
 File path: fs/fcb2/src/fcb_elem_info.c
 ##
 @@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include 
+#include 
+
+#include "fcb/fcb.h"
+#include "fcb_priv.h"
+
+/*
+ * Given offset in flash area, compute crc16 over the data.
+ */
+int
+fcb_elem_crc16(struct fcb_entry *loc, uint16_t *c16p)
+{
+uint8_t tmp_str[FCB_TMP_BUF_SZ];
+int blk_sz;
+uint16_t crc16;
+uint32_t off;
+uint32_t end;
+int rc;
+
+crc16 = 0x;
+
+off = loc->fe_data_off;
+end = loc->fe_data_off + loc->fe_data_len;
+for (; off < end; off += blk_sz) {
+blk_sz = end - off;
+if (blk_sz > sizeof(tmp_str)) {
+blk_sz = sizeof(tmp_str);
+}
+
+rc = fcb_read_from_sector(loc, off, tmp_str, blk_sz);
+if (rc) {
+return FCB_ERR_FLASH;
+}
+crc16 = crc16_ccitt(crc16, tmp_str, blk_sz);
+}
+*c16p = crc16;
+
+return 0;
+}
+
+int
+fcb_read_entry(struct fcb_entry *loc)
+{
+uint8_t buf[FCB_ENTRY_SIZE];
+uint8_t entry_crc;
+uint32_t entry_offset;
+uint32_t offset;
+uint16_t len;
+int rc;
+
+assert(loc != NULL);
+entry_offset = fcb_entry_location_in_range(loc);
+rc = flash_area_read_is_empty(>fe_range->fsr_flash_area,
+entry_offset, buf, sizeof(buf));
+if (rc < 0) {
+/* Error reading from flash */
+return FCB_ERR_FLASH;
+} else if (rc == 1) {
+/* Entry not filled on flash */
+return FCB_ERR_NOVAR;
+}
+/* Check entry CRC first */
+entry_crc = crc8_calc(crc8_init(), buf, FCB_ENTRY_SIZE - 1);
+if (entry_crc != buf[FCB_ENTRY_SIZE - 1]) {
+return FCB_ERR_CRC;
+}
+/* Sanity check for entry */
+offset = buf[0] | (buf[1] << 8) | (buf[2] << 16);
+len = buf[3] | (buf[4] << 8);
+if (offset < fcb_len_in_flash(loc->fe_range, sizeof(struct fcb_disk_area)) 
||
+len > FCB_MAX_LEN ||
+offset + len > entry_offset) {
+/* Entry was found but data stored does not make any sense
+ * report as CRC error so it can be skipped */
+return FCB_ERR_CRC;
+}
+
+/* Entry looks decent, pass to the caller */
+loc->fe_data_off = offset;
+loc->fe_data_len = len;
+
+return 0;
+}
+
+int
+fcb_elem_info(struct fcb_entry *loc)
+{
+int rc;
+uint16_t crc16;
+uint8_t fl_crc16[2];
 
 Review comment:
   changed to big endian, buffer is still kept access is changed to use 
get_be16()


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-09 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r232236580
 
 

 ##
 File path: fs/fcb2/test/src/fcb_test.c
 ##
 @@ -46,6 +46,7 @@ struct flash_sector_range test_fcb_ranges[] = {
 .fsr_first_sector = 0,
 .fsr_sector_size = 0x4000, /* 16 K */
 .fsr_sector_count = 4,
+.fsr_align = 1,
 
 Review comment:
   alignment could be retrieved using function which can't be use in this 
initialization.



This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-09 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r232235601
 
 

 ##
 File path: fs/fcb2/src/fcb_append.c
 ##
 @@ -0,0 +1,261 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include 
+
+#include "fcb/fcb.h"
+#include "fcb_priv.h"
+#include "crc/crc8.h"
+
+int
+fcb_new_sector(struct fcb *fcb, int cnt)
+{
+
+int new_sector = -1;
+int sector = fcb->f_active.fe_sector;
+do {
+sector = fcb_getnext_sector(fcb, sector);
+if (new_sector < 0) {
+new_sector = sector;
+}
+if (sector == fcb->f_oldest_sec) {
+new_sector = -1;
+break;
+}
+} while (--cnt >= 0);
+
+return new_sector;
+}
+
+/*
+ * Take one of the scratch blocks into use, if at all possible.
+ */
+int
+fcb_append_to_scratch(struct fcb *fcb)
+{
+int sector;
+int rc;
+struct flash_sector_range *range;
+
+sector = fcb_new_sector(fcb, 0);
+if (sector < 0) {
+return FCB_ERR_NOSPACE;
+}
+rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1);
+if (rc) {
+return rc;
+}
+range = fcb_get_sector_range(fcb, sector);
+fcb->f_active.fe_range = range;
+fcb->f_active.fe_sector = sector;
+fcb->f_active.fe_data_off =
+fcb_len_in_flash(range, sizeof(struct fcb_disk_area));
+fcb->f_active.fe_entry_num = 1;
+fcb->f_active_id++;
+return FCB_OK;
+}
+
+static inline int
+sector_offset_to_flash_area_offset(const struct flash_sector_range *range,
+int sector, int offset)
+{
+return offset + ((sector - range->fsr_first_sector) * 
range->fsr_sector_size);
+}
+
+static inline int
+fcb_sector_flash_offset(const struct fcb_entry *loc)
+{
+return (loc->fe_sector - loc->fe_range->fsr_first_sector) *
+loc->fe_range->fsr_sector_size;
+}
+
+int
+fcb_write_to_sector(struct fcb_entry *loc, int off, const void *buf, int len)
+{
+/* For negative offsets write from the end of sector */
+if (off < 0) {
+off += loc->fe_range->fsr_sector_size;
+}
+/* Truncate writes beyond sector */
+if (off + len > loc->fe_range->fsr_sector_size) {
+len = loc->fe_range->fsr_sector_size - off;
+}
+return flash_area_write(>fe_range->fsr_flash_area,
+fcb_sector_flash_offset(loc) + off, buf, len);
+}
+
+int
+fcb_read_from_sector(struct fcb_entry *loc, int off, void *buf, int len)
+{
+/* For negative offsets read from the end of sector */
+if (off < 0) {
+off += loc->fe_range->fsr_sector_size;
+}
+/* Truncate read beyond sector */
+if (off + len > loc->fe_range->fsr_sector_size) {
+len = loc->fe_range->fsr_sector_size - off;
+}
+return flash_area_read(>fe_range->fsr_flash_area,
+fcb_sector_flash_offset(loc) + off, buf, len);
+}
+
+int fcb_entry_location_in_range(const struct fcb_entry *loc)
 
 Review comment:
   done


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-09 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r232235677
 
 

 ##
 File path: fs/fcb2/src/fcb_append.c
 ##
 @@ -0,0 +1,261 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include 
+
+#include "fcb/fcb.h"
+#include "fcb_priv.h"
+#include "crc/crc8.h"
+
+int
+fcb_new_sector(struct fcb *fcb, int cnt)
+{
+
+int new_sector = -1;
+int sector = fcb->f_active.fe_sector;
+do {
+sector = fcb_getnext_sector(fcb, sector);
+if (new_sector < 0) {
+new_sector = sector;
+}
+if (sector == fcb->f_oldest_sec) {
+new_sector = -1;
+break;
+}
+} while (--cnt >= 0);
+
+return new_sector;
+}
+
+/*
+ * Take one of the scratch blocks into use, if at all possible.
+ */
+int
+fcb_append_to_scratch(struct fcb *fcb)
+{
+int sector;
+int rc;
+struct flash_sector_range *range;
+
+sector = fcb_new_sector(fcb, 0);
+if (sector < 0) {
+return FCB_ERR_NOSPACE;
+}
+rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1);
+if (rc) {
+return rc;
+}
+range = fcb_get_sector_range(fcb, sector);
+fcb->f_active.fe_range = range;
+fcb->f_active.fe_sector = sector;
+fcb->f_active.fe_data_off =
+fcb_len_in_flash(range, sizeof(struct fcb_disk_area));
+fcb->f_active.fe_entry_num = 1;
+fcb->f_active_id++;
+return FCB_OK;
+}
+
+static inline int
+sector_offset_to_flash_area_offset(const struct flash_sector_range *range,
 
 Review comment:
   function was not used, removed


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-09 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r232235567
 
 

 ##
 File path: fs/fcb2/src/fcb_area_info.c
 ##
 @@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "fcb/fcb.h"
+#include "fcb_priv.h"
+
+int
+fcb_area_info(struct fcb *fcb, int sector, int *elemsp, int *bytesp)
+{
+struct fcb_entry loc;
+struct fcb_sector_info info;
+int rc;
+int elems = 0;
+int bytes = 0;
+
+fcb_get_sector_info(fcb, sector, );
 
 Review comment:
   checks added


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-09 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r232235501
 
 

 ##
 File path: fs/fcb2/src/fcb_getnext.c
 ##
 @@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include 
+
+#include "fcb/fcb.h"
+#include "fcb_priv.h"
+
+int
+fcb_getnext_in_area(struct fcb *fcb, struct fcb_entry *loc)
+{
+int rc = FCB_ERR_CRC;
+int off;
+int len;
+
+while (rc == FCB_ERR_CRC) {
+len = loc->fe_data_len;
+off = loc->fe_data_off;
+loc->fe_data_len = 0;
+loc->fe_entry_num++;
+rc = fcb_elem_info(loc);
+if (len) {
+loc->fe_data_off = off + fcb_len_in_flash(loc->fe_range, len) +
++ fcb_len_in_flash(loc->fe_range, 2);
 
 Review comment:
   removed


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-08 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r231851517
 
 

 ##
 File path: fs/fcb2/src/fcb_append.c
 ##
 @@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include 
+
+#include "fcb/fcb.h"
+#include "fcb_priv.h"
+#include "crc/crc8.h"
+
+int
+fcb_new_sector(struct fcb *fcb, int cnt)
+{
+
+int new_sector = -1;
+int sector = fcb->f_active.fe_sector;
+do {
+sector = fcb_getnext_sector(fcb, sector);
+if (new_sector < 0) {
+new_sector = sector;
+}
+if (sector == fcb->f_oldest_sec) {
+new_sector = -1;
+break;
+}
+} while (--cnt >= 0);
+
+return new_sector;
+}
+
+/*
+ * Take one of the scratch blocks into use, if at all possible.
+ */
+int
+fcb_append_to_scratch(struct fcb *fcb)
+{
+int sector;
+int rc;
+struct sector_range *range;
+
+sector = fcb_new_sector(fcb, 0);
+if (sector < 0) {
+return FCB_ERR_NOSPACE;
+}
+rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1);
+if (rc) {
+return rc;
+}
+range = fcb_get_sector_range(fcb, sector);
+fcb->f_active.fe_range = range;
+fcb->f_active.fe_sector = sector;
+fcb->f_active.fe_data_off =
+fcb_len_in_flash(range, sizeof(struct fcb_disk_area));
+fcb->f_active.fe_entry_num = 1;
+fcb->f_active_id++;
+return FCB_OK;
+}
+
+static inline int
+sector_offset_to_flash_area_offset(const struct sector_range *range, int 
sector,
+int offset)
+{
+return offset + ((sector - range->sr_first_sector) * 
range->sr_sector_size);
+}
+
+static inline int
+fcb_sector_flash_offset(const struct fcb_entry *loc)
+{
+return (loc->fe_sector - loc->fe_range->sr_first_sector) *
+loc->fe_range->sr_sector_size;
+}
+
+int
+fcb_write_to_sector(struct fcb_entry *loc, int off, const void *buf, int len)
+{
+/* For negative offsets write from the end of sector */
+if (off < 0) {
+off += loc->fe_range->sr_sector_size;
+}
+/* Truncate writes beyond sector */
+if (off + len > loc->fe_range->sr_sector_size) {
+len = loc->fe_range->sr_sector_size - off;
+}
+return flash_area_write(>fe_range->sr_flash_area,
+fcb_sector_flash_offset(loc) + off, buf, len);
+}
+
+int
+fcb_read_from_sector(struct fcb_entry *loc, int off, void *buf, int len)
+{
+/* For negative offsets read from the end of sector */
+if (off < 0) {
+off += loc->fe_range->sr_sector_size;
+}
+/* Truncate read beyond sector */
+if (off + len > loc->fe_range->sr_sector_size) {
+len = loc->fe_range->sr_sector_size - off;
+}
+return flash_area_read(>fe_range->sr_flash_area,
+fcb_sector_flash_offset(loc) + off, buf, len);
+}
+
+int fcb_entry_location_in_range(const struct fcb_entry *loc)
+{
+const struct sector_range *range = loc->fe_range;
+
+return range->sr_sector_size * (1 + loc->fe_sector - 
range->sr_first_sector) -
+(loc->fe_entry_num * fcb_len_in_flash(loc->fe_range, FCB_ENTRY_SIZE));
+}
+
+int fcb_active_sector_free_space(const struct fcb *fcb)
+{
+const struct fcb_entry *active = >f_active;
+const struct sector_range *range = active->fe_range;
+
+return range->sr_sector_size - active->fe_data_off -
+(active->fe_entry_num * FCB_ENTRY_SIZE);
+}
+
+int
+fcb_write(struct fcb_entry *loc, uint16_t off, void *buf, uint16_t len)
+{
+int pos = loc->fe_data_off + off;
+/* Make sure tha write does not exceed lenght declared in fcb_append */
+if (off + len > loc->fe_data_len) {
+len = loc->fe_data_len - off;
+}
+return fcb_write_to_sector(loc, pos, buf, len);
+}
+
+int
+fcb_read(struct fcb_entry *loc, uint16_t off, void *buf, uint16_t len)
+{
+int pos = loc->fe_data_off + off;
+/* Make sure tha read is only from entry data */
+if (off + len > loc->fe_data_len) {
+len = loc->fe_data_len - off;
+}
+return fcb_read_from_sector(loc, pos, buf, len);
+}
+
+int fcb_element_length_in_flash(const struct fcb_entry *loc, int len)
+{
+return 

[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-08 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r231851486
 
 

 ##
 File path: fs/fcb2/src/fcb_append.c
 ##
 @@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include 
+
+#include "fcb/fcb.h"
+#include "fcb_priv.h"
+#include "crc/crc8.h"
+
+int
+fcb_new_sector(struct fcb *fcb, int cnt)
+{
+
+int new_sector = -1;
+int sector = fcb->f_active.fe_sector;
+do {
+sector = fcb_getnext_sector(fcb, sector);
+if (new_sector < 0) {
+new_sector = sector;
+}
+if (sector == fcb->f_oldest_sec) {
+new_sector = -1;
+break;
+}
+} while (--cnt >= 0);
+
+return new_sector;
+}
+
+/*
+ * Take one of the scratch blocks into use, if at all possible.
+ */
+int
+fcb_append_to_scratch(struct fcb *fcb)
+{
+int sector;
+int rc;
+struct sector_range *range;
+
+sector = fcb_new_sector(fcb, 0);
+if (sector < 0) {
+return FCB_ERR_NOSPACE;
+}
+rc = fcb_sector_hdr_init(fcb, sector, fcb->f_active_id + 1);
+if (rc) {
+return rc;
+}
+range = fcb_get_sector_range(fcb, sector);
+fcb->f_active.fe_range = range;
+fcb->f_active.fe_sector = sector;
+fcb->f_active.fe_data_off =
+fcb_len_in_flash(range, sizeof(struct fcb_disk_area));
+fcb->f_active.fe_entry_num = 1;
+fcb->f_active_id++;
+return FCB_OK;
+}
+
+static inline int
+sector_offset_to_flash_area_offset(const struct sector_range *range, int 
sector,
+int offset)
+{
+return offset + ((sector - range->sr_first_sector) * 
range->sr_sector_size);
+}
+
+static inline int
+fcb_sector_flash_offset(const struct fcb_entry *loc)
+{
+return (loc->fe_sector - loc->fe_range->sr_first_sector) *
+loc->fe_range->sr_sector_size;
+}
+
+int
+fcb_write_to_sector(struct fcb_entry *loc, int off, const void *buf, int len)
+{
+/* For negative offsets write from the end of sector */
+if (off < 0) {
+off += loc->fe_range->sr_sector_size;
+}
+/* Truncate writes beyond sector */
+if (off + len > loc->fe_range->sr_sector_size) {
+len = loc->fe_range->sr_sector_size - off;
+}
+return flash_area_write(>fe_range->sr_flash_area,
+fcb_sector_flash_offset(loc) + off, buf, len);
+}
+
+int
+fcb_read_from_sector(struct fcb_entry *loc, int off, void *buf, int len)
+{
+/* For negative offsets read from the end of sector */
+if (off < 0) {
+off += loc->fe_range->sr_sector_size;
+}
+/* Truncate read beyond sector */
+if (off + len > loc->fe_range->sr_sector_size) {
+len = loc->fe_range->sr_sector_size - off;
+}
+return flash_area_read(>fe_range->sr_flash_area,
+fcb_sector_flash_offset(loc) + off, buf, len);
+}
+
+int fcb_entry_location_in_range(const struct fcb_entry *loc)
+{
+const struct sector_range *range = loc->fe_range;
+
+return range->sr_sector_size * (1 + loc->fe_sector - 
range->sr_first_sector) -
+(loc->fe_entry_num * fcb_len_in_flash(loc->fe_range, FCB_ENTRY_SIZE));
+}
+
+int fcb_active_sector_free_space(const struct fcb *fcb)
+{
+const struct fcb_entry *active = >f_active;
+const struct sector_range *range = active->fe_range;
+
+return range->sr_sector_size - active->fe_data_off -
+(active->fe_entry_num * FCB_ENTRY_SIZE);
 
 Review comment:
   good point. fixed


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-08 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r231851395
 
 

 ##
 File path: fs/fcb2/include/fcb/fcb.h
 ##
 @@ -0,0 +1,214 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef __SYS_FCB_H_
+#define __SYS_FCB_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \defgroup FCB Flash circular buffer.
+ * @{
+ */
+
+#include 
+#include 
+
+#include "os/mynewt.h"
+#include "flash_map/flash_map.h"
+
+#define FCB_MAX_LEN(CHAR_MAX | CHAR_MAX << 7) /* Max length of element */
+
+#define FCB_SECTOR_OLDEST UINT16_MAX
+
+struct fcb;
+
+/**
+ * Entry location point to sector, and offset
+ * within that sector.
+ */
+struct fcb_entry {
+struct fcb *fe_fcb;
+struct sector_range *fe_range;  /* ptr to area within fcb->f_ranages */
+uint16_t fe_sector; /* sector number in fcb flash */
+uint16_t fe_data_len;   /* size of data area */
+uint32_t fe_data_off;   /* start of data in sector */
+uint16_t fe_entry_num;  /* entry number in sector */
+};
+
+/* Number of bytes needed for fcb_sector_entry on flash */
+#define FCB_ENTRY_SIZE  6
+#define FCB_CRC_LEN 2
+
+struct fcb {
+/* Caller of fcb_init fills this in */
+uint32_t f_magic;   /* As placed on the disk */
+uint8_t f_version;  /* Current version number of the data */
+uint8_t f_scratch_cnt;  /* How many sectors should be kept empty */
+uint8_t f_range_cnt;/* Number of elements in range array */
+uint16_t f_sector_cnt;  /* Number of sectors used by fcb */
+struct sector_range *f_ranges;
+
+/* Flash circular buffer internal state */
+struct os_mutex f_mtx; /* Locking for accessing the FCB data */
+uint16_t f_oldest_sec;
+struct fcb_entry f_active;
+uint16_t f_active_id;
+uint16_t f_sector_entries; /* Number of entries in current sector */
+//uint8_t f_align;/* writes to flash have to aligned to this */
+};
+
+struct fcb_sector_info {
+struct sector_range *si_range;  /* Sector range */
+uint32_t si_sector_offset;  /* Sector offset in fcb */
+uint16_t si_sector_in_range;/* Sector number relative to si_range */
+};
+
+/**
+ * Error codes.
+ */
+#define FCB_OK   0
+#define FCB_ERR_ARGS-1
+#define FCB_ERR_FLASH   -2
+#define FCB_ERR_NOVAR   -3
+#define FCB_ERR_NOSPACE -4
+#define FCB_ERR_NOMEM   -5
+#define FCB_ERR_CRC -6
+#define FCB_ERR_MAGIC   -7
+#define FCB_ERR_VERSION -8
+
+int fcb_init(struct fcb *fcb);
+
+/*
+ * Initialize fcb for specific flash area
+ *
+ * Function initializes FCB structure with data taken from specified flash
+ * area.
+ * If FCB was not initialized before in this area, area will be erased.
+ *
+ * @param fcbFcb to initialize
+ * @param flash_area_id  flash area for this fcb
+ * @param versionversion of fcb
+ *
+ */
+int fcb_init_flash_area(struct fcb *fcb, int flash_area_id, uint8_t version);
+
+/**
+ * fcb_log is needed as the number of entries in a log
+ */
+struct fcb_log {
+struct fcb fl_fcb;
+uint8_t fl_entries;
+
+#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)
+/* Internal - tracking storage use */
+uint32_t fl_watermark_off;
+#endif
+};
+
+/**
+ * fcb_append() appends an entry to circular buffer. When writing the
+ * contents for the entry, fcb_write() with fcb_entry filled by.
+ * fcb_append(). When you're finished, call fcb_append_finish() with
+ * loc as argument.
+ */
+int fcb_append(struct fcb *fcb, uint16_t len, struct fcb_entry *loc);
+int fcb_write(struct fcb_entry *loc, uint16_t off, void *buf, uint16_t len);
+int fcb_append_finish(struct fcb_entry *append_loc);
+
+/**
+ * Walk over all log entries in FCB, or entries in a given flash_area.
+ * cb gets called for every entry. If cb wants to stop the walk, it should
+ * return non-zero value.
+ *
+ * Entry data can be read using fcb_read().
+ */
+typedef int (*fcb_walk_cb)(struct fcb_entry *loc, void *arg);
+int fcb_walk(struct fcb *, int sector, fcb_walk_cb cb, void *cb_arg);
+int fcb_getnext(struct fcb *fcb, struct fcb_entry *loc);
+int fcb_read(struct fcb_entry *loc, uint16_t off, void *buf, uint16_t 

[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-08 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r231851307
 
 

 ##
 File path: fs/fcb2/src/fcb.c
 ##
 @@ -0,0 +1,370 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include 
+#include 
+
+#include "fcb/fcb.h"
+#include "fcb_priv.h"
+#include "string.h"
+
+int
+fcb_init(struct fcb *fcb)
+{
+struct sector_range *range;
+struct sector_range *newest_srp = NULL;
+int rc;
+int i;
+int oldest = -1, newest = -1;
+int oldest_sec = -1, newest_sec = -1;
+struct fcb_disk_area fda;
+
+if (!fcb->f_ranges || fcb->f_sector_cnt - fcb->f_scratch_cnt < 1) {
+return FCB_ERR_ARGS;
+}
+
+/* Fill last used, first used */
+for (i = 0; i < fcb->f_sector_cnt; i++) {
+range = fcb_get_sector_range(fcb, i);
+/* Require alignment to be a power of two.  Some code depends on this
+ * assumption.
+ */
+assert((range->sr_align & (range->sr_align - 1)) == 0);
+rc = fcb_sector_hdr_read(fcb, range, i, );
+if (rc < 0) {
+return rc;
+}
+if (rc == 0) {
+continue;
+}
+if (oldest < 0) {
+oldest = newest = fda.fd_id;
+oldest_sec = newest_sec = i;
+newest_srp = range;
+continue;
+}
+if (FCB_ID_GT(fda.fd_id, newest)) {
+newest = fda.fd_id;
+newest_sec = i;
+newest_srp = range;
+} else if (FCB_ID_GT(oldest, fda.fd_id)) {
+oldest = fda.fd_id;
+oldest_sec = i;
+}
+}
+if (oldest < 0) {
+/*
+ * No initialized areas.
+ */
+oldest_sec = newest_sec = 0;
+newest_srp = fcb->f_ranges;
+rc = fcb_sector_hdr_init(fcb, newest_sec, 0);
+if (rc) {
+return rc;
+}
+newest = oldest = 0;
+}
+fcb->f_oldest_sec = oldest_sec;
+fcb->f_active.fe_range = newest_srp;
+fcb->f_active.fe_sector = newest_sec;
+fcb->f_active.fe_data_off =
+fcb_len_in_flash(newest_srp, sizeof(struct fcb_disk_area));
+fcb->f_active.fe_entry_num = 0;
+fcb->f_active_id = newest;
+
+while (1) {
+rc = fcb_getnext_in_area(fcb, >f_active);
+if (rc == FCB_ERR_NOVAR) {
+rc = FCB_OK;
+break;
+}
+if (rc != 0) {
+break;
+}
+}
+os_mutex_init(>f_mtx);
+return rc;
+}
+
+int
+fcb_free_sector_cnt(struct fcb *fcb)
+{
+int i;
+int sector;
+
+sector = fcb->f_active.fe_sector;
+for (i = 0; i < fcb->f_sector_cnt; i++) {
+sector = fcb_getnext_sector(fcb, sector);
+if (sector == fcb->f_oldest_sec) {
+break;
+}
+}
+return i;
+}
+
+int
+fcb_is_empty(struct fcb *fcb)
+{
+return (fcb->f_active.fe_sector == fcb->f_oldest_sec &&
+fcb->f_active.fe_data_off ==
+fcb_len_in_flash(fcb->f_active.fe_range, sizeof(struct 
fcb_disk_area)));
+}
+
+struct sector_range *
+fcb_get_sector_range(const struct fcb *fcb, int sector)
+{
+int i;
+struct sector_range *srp = fcb->f_ranges;
+
+if (FCB_SECTOR_OLDEST == sector) {
+sector = fcb->f_oldest_sec;
+}
+for (i = 0; i < fcb->f_range_cnt; ++i, ++srp) {
+if (srp->sr_sector_count <= sector) {
+sector -= srp->sr_sector_count;
+continue;
+}
+return srp;
+}
+return NULL;
+}
+
+/**
+ * Initialize erased sector for use.
+ */
+int
+fcb_sector_hdr_init(struct fcb *fcb, int sector, uint16_t id)
+{
+struct fcb_disk_area fda;
+struct fcb_sector_info info;
+struct sector_range *range;
+int sector_in_range;
+int rc;
+
+rc = fcb_get_sector_info(fcb, sector, );
+if (rc) {
+return rc;
+}
+range = info.si_range;
+sector_in_range = sector - range->sr_first_sector;
+
+fda.fd_magic = fcb->f_magic;
+fda.fd_ver = fcb->f_version;
+fda._pad = 0xff;
+fda.fd_id = id;
+
+assert(sector_in_range >= 0 && sector_in_range < range->sr_sector_count);
+rc = flash_area_write(>sr_flash_area,
+

[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-08 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r231851164
 
 

 ##
 File path: sys/flash_map/include/flash_map/flash_map.h
 ##
 @@ -52,6 +52,15 @@ struct flash_area {
 uint32_t fa_size;
 };
 
+struct sector_range {
+struct flash_area sr_flash_area;
+uint32_t sr_range_start;
+uint16_t sr_first_sector;
+uint16_t sr_sector_count;
+uint32_t sr_sector_size;
+uint8_t sr_align;
 
 Review comment:
   I decided against. I know that this field could be easily forgotten. I did 
it. But it is filled in one place only and accessed many times so calling 
function each time even though value is know all the time makes me unhappy.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] kasjer commented on a change in pull request #1498: fcb2: second generation of fcb

2018-11-08 Thread GitBox
kasjer commented on a change in pull request #1498: fcb2: second generation of 
fcb
URL: https://github.com/apache/mynewt-core/pull/1498#discussion_r231850607
 
 

 ##
 File path: fs/fcb2/test/src/testcases/fcb_test_append.c
 ##
 @@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "fcb_test.h"
+
+TEST_CASE(fcb_test_append)
+{
+int rc;
+struct fcb *fcb;
+struct fcb_entry loc;
+uint8_t test_data[128];
+int i;
+int j;
+int var_cnt;
+
+#if 0
 
 Review comment:
   commented out code remove. It was there is fcb, I have not tested it enabled 
so removed.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services